JS中的扩展属性?

时间:2011-09-19 12:22:26

标签: javascript

我们可以在运行时在javascript中将属性与对象关联起来 就像我们在C#中做的那样。  喜欢

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

abc obj;

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

是的,你可以在JS中做到这一点。然而,新属性'c'仅对该类的特定实例有效。

相关问题