2013-03-16 45 views
0

我正在尋找動態設置對象的屬性。看下面的例子。我發現setAttribute方法只適用於DOM元素。有沒有辦法我可以動態地爲js對象設置屬性?如何動態設置javascript對象的屬性

+3

你在尋找'this.scope [type] = true'嗎? – DCoder 2013-03-16 08:27:33

+0

是的,只是想通了。謝謝。 – Amyth 2013-03-16 08:27:54

+0

[Dynamic object property name]的可能重複(http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – 2013-03-16 09:28:09

回答

1
function testFunc(type, scope){ 
    this.scope = scope; 
    this.scope[type]= true; 
    this.doSome = function(){return //Something;} 
} 

應該這樣做。

+0

是的確。謝謝@mpm。 – Amyth 2013-03-16 08:38:04