2016-08-18 14 views
2

私はこの本を読んでいて、苦労や他の多くのリソースを使って、ここでそれを作った(それだけでは難しかったが、 )。第7章、雄弁なJavaScript:LifelikeWorld

これは私が理解しにくい見つけるコードです:

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); 
    } 
}; 

var actionTypes = Object.create(null); // actionTypesはnullオブジェクトrigthをcreas?

var handled、私はその変数の周りに頭を浮かべて、私は概念を打ち負かす、アクション、actiontypeが真であるかどうかをチェックするか? actionTypes [action.type] => actionTypeはnullオブジェクトではありませんでしたか?

答えて

1

Object.create(null)は、nullというオブジェクトをプロトタイプとして作成します。つまり、オブジェクトリテラル({})またはnew Object()とは異なり、プロパティはObject.prototypeから継承されません。

handled変数はaction変数がtruthyときtrueは、actionTypes変数キーaction.typeactionTypes[action.type].call(this, critter, vector, action)戻りtruthy値を含んでいます。