如何缓存最顶层的范围,以便以后在原型中更深入地使用,如下所示:
var Game = function(id){
this.id = id;
};
Game.prototype = {
board : {
init: function(){
// obviously "this" isn't the instance itself, but will be "board"
console.log(this.id);
}
}
}
var game = new Game('123');
game.board.init(); // should output "123"
更新:
好吧,现在我考虑一下,我可以使用apply
/call
并传递上下文......
game.board.init.apply(game);