我希望动态设置对象的属性。请看下面的示例。
function testFunc(type, scope){
this.scope = scope;
this.scope.setAttribute(type, true);
this.doSome = function(){return //Something;}
}
但我意识到 setAttribute
方法仅适用于 DOM 元素。有没有办法可以动态地为 js 对象设置属性?
最佳答案
function testFunc(type, scope){
this.scope = scope;
this.scope[type]= true;
this.doSome = function(){return //Something;}
}
应该可以解决问题。
关于javascript - 如何动态设置javascript对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447183/