如何以正确的方式调用rest()函数

时间:2018-06-02 16:49:59

标签: javascript function callback

这种逻辑的预期结果是 每次我调用函数rest() 该生物的健康应该恢复到10 当我尝试调用函数休息时 我得到了 未捕获的TypeError:无法读取未定义的属性“健康”

(我想将函数绑定到DOM,当单击'rest'图像时,函数rest()被调用并且 该生物的健康恢复)

 const creature = {
   name: 'apiGong',
   alive: true,
  inventory: [],
   health: 5,
  item: {type: "claws", damage: 1,
  },
 }


   function rest(creature){
    let health = creature.health
   while (health < 10){
    health++
  }
     return health;
   }

0 个答案:

没有答案