gpt4 book ai didi

javascript - 在 node.js javascript 为什么我不能添加到子函数?

转载 作者:行者123 更新时间:2023-11-29 22:38:56 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com