2016-08-18 23 views
2

我一直在閱讀這本書,並且帶着很多困難和其他資源,通過這裏完成了它(它本身太難了,但它仍然非常有用,連同其他資源)。Chapter 7,eloquent 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 CREAS空對象分辯?

var handled,我無法理解這個變量,我進入了概念,它是否檢查動作,動作類型是否屬實?並且,actionTypes [action.type] => actionType不是一個空對象?

回答

1

Object.create(null)null爲原型創建對象。這意味着不像對象字面量({})或new Object()它不會繼承Object.prototype的屬性。

handled變量是trueaction變量是truthy,actionTypes變量包含鍵action.typeactionTypes[action.type].call(this, critter, vector, action)返回truthy值。