2011-09-19 115 views
0

我們可以在運行時將屬性與對象關聯起來,就像我們在C#中所做的那樣: 。 像JS中的擴展屬性?

class abc 
{ 
a =1; 
b=2; 
} 

abc obj; 

obj.a // is right 
//but can we do 
obj.c //.......... by any mean 

回答

2
function abc(){ 
    this.a = 1; 
    this.b = 2; 
} 
var obj = new abc(); 
var obj2 = new abc(); 
obj.c = "something"; // will affect only this instance 
obj2.prototype.d = "something else"; // this will influence all abc instances 
alert(obj.d); 
+0

感謝您的回覆。現在,如果我們想要在運行時將數組或函數附加到對象,是否可行? –

1

是的,你可以做到這一點的JS。然而,這個新的屬性'c'只對那個類的特定實例有效。