在node.js javascript为什么我不能添加到子function?
game_state = function(){ this.players = function() { this.x = 0; }; } game_state.players['test'] = 1;
为什么这个失败,但
game_state['test'] = 1;
才不是?
我试图在node.js中的情况下,它不明确。 谢谢你的帮助。
game_state
是一个(构造函数)函数。 函数的实例有一个玩家属性,但是函数本身没有。 我想你可能想要:
game_state = new (function() { this.players = new (function() { this.x = 0; })(); })(); game_state.players['test'] = 1;
编辑:同样适用于内部function。 另外,在这两种情况下,您都可以使用对象文字。