game_state = function(){
this.players = function() {
this.x = 0;
};
}
game_state.players['test'] = 1;
为什么会失败
game_state['test'] = 1;
不是吗?
我正在 node.js 中尝试这个,以防它不清楚。感谢您的帮助。
最佳答案
game_state
是一个(构造函数)函数。该函数的实例具有 players 属性,但函数本身没有。我想你可能想要:
game_state = new (function()
{
this.players = new (function()
{
this.x = 0;
})();
})();
game_state.players['test'] = 1;
编辑:这同样适用于内部函数。此外,在这两种情况下,您都可以使用对象字面量。
关于javascript - 在 node.js javascript 为什么我不能添加到子函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3901640/