javascript原型式继承如何理解

1、说明

利用空对象作为中介,将某个对象直接分配给空对象构造函数的原型。

2、缺点

原型链继承多个实例的引用类型属性指向相同,可能会被篡改。

不能传递参数。

3、实例

varperson={
name:"Nicholas",
friends:["Shelby","Court","Van"]
};

varanotherPerson=object(person);
anotherPerson.name="Greg";
anotherPerson.friends.push("Rob");

varyetAnotherPerson=object(person);
yetAnotherPerson.name="Linda";
yetAnotherPerson.friends.push("Barbie");

alert(person.friends);//"Shelby,Court,Van,Rob,Barbie"
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享