gpt4 book ai didi

javascript - 通过变量更新属性方法

转载 作者:行者123 更新时间:2023-11-30 13:17:55 24 4
gpt4 key购买 nike

我想通过调用此代码的方法更改对象的属性:

MAP = MAP || {
level: 0
updateProperty(property, value){
if (this.hasOwnProperty(property){
this.property = value
}
}
}

但是当我触发这段代码时,我在我的对象中创建了名为“property”的新属性。但我想将“属性”视为变量,而不是新属性!

最佳答案

你可能想要这个(方括号 this[property] = value;)

var MAP = MAP || {};
MAP = {
level: 0,
updateProperty(property, value) {
if (this.hasOwnProperty(property)) {
this[property] = value;
}
}
};

关于javascript - 通过变量更新属性方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330309/

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