第7章,雄辩的JavaScript:LifelikeWorld

第7章,雄辩的JavaScript:LifelikeWorld,javascript,class,oop,prototype,javascript-objects,Javascript,Class,Oop,Prototype,Javascript Objects,我一直在读这本书,并且,用了大量的艰辛和其他资源,在这里度过了难关(单是这本书太难了,但它仍然非常有用,还有其他资源) 这是我发现很难理解的代码: function LifelikeWorld(map, legend) { World.call(this, map, legend); } LifelikeWorld.prototype = Object.create(World.prototype); var actionTypes = Object.create(null); Life

我一直在读这本书,并且,用了大量的艰辛和其他资源,在这里度过了难关(单是这本书太难了,但它仍然非常有用,还有其他资源)

这是我发现很难理解的代码:

function LifelikeWorld(map, legend) {
  World.call(this, map, legend);
}
LifelikeWorld.prototype = Object.create(World.prototype);

var actionTypes = Object.create(null);

LifelikeWorld.prototype.letAct = function(critter, vector) {
  var action = critter.act(new View(this, vector));
  var handled = action &&
    action.type in actionTypes &&
    actionTypes[action.type].call(this, critter,
                                  vector, action);
  if (!handled) {
    critter.energy -= 0.2;
    if (critter.energy <= 0)
      this.grid.set(vector, null);
  }
};
函数LifelikeWorld(地图、图例){
调用(此、地图、图例);
}
LifelikeWorld.prototype=Object.create(World.prototype);
var actionTypes=Object.create(null);
LifelikeWorld.prototype.letAct=函数(生物,向量){
var action=critter.act(新视图(这个,向量));
var=action&&
actionTypes中的action.type&&
actionTypes[action.type]。调用(这个,小动物,
向量,动作);
如果(!已处理){
生物能量-=0.2;
如果(critter.energy actionType)不是空对象?

对象。创建(空)
创建一个原型为
null
的对象。这意味着与对象文本(
{}
)或
新对象()
不同,它不会从
对象.prototype
继承属性

handled
变量为
true
action
变量为truthy时,
actionTypes
变量包含键
action.type
actionTypes[action.type]。调用(this,critter,vector,action)
返回truthy值