JavaScript中,不是在实例中修改prototype属性会在该实例中隐藏类的prototype属性,而其他实例不受影响吗?
function Person(){ } Person.prototype = { name : "Nicholas", age : 29, friends : ["Shelby", "Court"] }; var person1 = new Person(); var person2 = new Person(); person1.friends.push("Van"); alert(person1.friends); //"Shelby,Court,Van" alert(person2.friends); //"Shelby,Court,Van" person1.name = 'tom'; alert(person1.name); //tom alert(person2.name); //Nicholas
请看代码,
person1.friends.push("Van");给friends数组添加了元素“van”。person1.friends、person2.friends都输出了"Shelby,Court,Van"。
(是不是说明prototype里的数组也改变了?)
person1.name = 'tom';在person1中将name属性改成了‘tom’。
person1.name输出了‘tom’,person2.name则输出了‘Nicholas’。
说明prototype里的name没有改变.
请问这该如何解释?
谢谢
相关分类