我一直认为一个函数的原型是在所有对象之间共享的,在某种意义上是通过引用。因此,如果您更改原型的某个属性的值,则共享该原型的所有对象的值也会随之更改。例如,在下面的例子中,属性栏似乎不是在所有对象之间共享,而是被复制。是这样的吗?构造器原型的属性是在创建时简单地复制到所有类对象,还是通过链接共享?
function foo()
{
this.bar = 1;
}
function derived() { }
derived.prototype = new foo()
object1 = new derived()
object2 = new derived()
object1.bar = 2;
//but notice if I had said here derived.prototype.bar = 3, object1.bar would still equal 2 but object2.bar would equal 3
alert(object2.bar) // this prints 1;发布于 2011-10-03 13:49:54
例如,您具有以下代码:
function Animal() {
}
Animal.prototype.name="animal";
function Dog() {
}
Dog.prototype = new Animal
Dog.prototype.constructor=Dog;
Dog.prototype.name="dog";
object1 = new Animal();
object2 = new Dog();因此,你有两个对象实例,看起来像(你可以在Chrome devtools或FF firebug或...中检查这一点):
object1:
__proto__: (is ref into an Animal.prototype object)
constructor: function Animal()
name: "animal"
__proto__: Object (is ref into an Object.prototype object)
object2:
__proto__: (is ref into an Dog.prototype object)
constructor: function Dog()
name: "dog"
__proto__: (is ref into an Animal.prototype object)
constructor: function Animal()
name: "animal"
__proto__: (is ref into an Object.prototype object)当你运行下一段代码时(例如):
alert(object1.name); // displayed "animal"
alert(object2.name); // displayed "dog"发生了什么? 1) Javascript在对象实例中查找属性名称(在object1或object2中)。2)未找到时,在对象实例proto属性中查找(与类函数原型相同)。3)未发现时,查找proto of proto和next和next,而未找到name属性和其他proto。如果找到作为搜索结果的属性,则返回value,如果未找到,则返回undefined。
如果执行下一段代码,会发生什么:
object2.name = "doggy";因此,对于object2,您将拥有:
object2:
name: "doggy"
__proto__: (is ref into an Dog.prototype object)
constructor: function Dog()
name: "dog"
__proto__: (is ref into an Animal.prototype object)
constructor: function Animal()
name: "animal"
__proto__: (is ref into an Object.prototype object)属性直接分配给实例对象,但原型对象保持不变。当您执行以下命令时:
alert(object1.name); // displayed "animal"
alert(object2.name); // displayed "doggy"当您需要创建|更改类的共享属性时,可以使用下一个算法中的一个: 1)
Animal.prototype.secondName="aaa";
alert(object1.secondName); // displayed "aaa"
alert(object2.secondName); // displayed "aaa"
Animal.prototype.secondName="bbb";
alert(object1.secondName); // displayed "bbb"
alert(object2.secondName); // displayed "bbb"
// but
Animal.prototype.secondName="ccc";
object1.secondName="ddd";
alert(object1.secondName); // displayed "ccc"
alert(object2.secondName); // displayed "ddd"2)在function类的原型中创建object类型的属性,并为该对象的属性赋值。
Animal.prototype.propObject={thirdName:"zzz"};
alert(object1.propObject.thirdName); // displayed "zzz"
alert(object2.propObject.thirdName); // displayed "zzz"
Animal.prototype.propObject.thirdName="yyy";
alert(object1.propObject.thirdName); // displayed "yyy"
alert(object2.propObject.thirdName); // displayed "yyy"
object1.propObject.thirdName="xxx";
alert(object1.propObject.thirdName); // displayed "xxx"
alert(object2.propObject.thirdName); // displayed "xxx"
object2.propObject.thirdName="www";
alert(object1.propObject.thirdName); // displayed "www"
alert(object2.propObject.thirdName); // displayed "www"https://stackoverflow.com/questions/7631196
复制相似问题