寄生式继承
实例1:
function creatAnother(original){ var clone = new Object(original); clone.say = function(){ console.log(this.name); } return clone; } var person1 = { name:'zhangsan', friends:['jim','tom','van'] }; var person2 = { name:'lisi', friends:['klkx','qu','you'] }; var p1 = creatAnother( person1 ); var p2 = creatAnother( person2 ); p1.say(); p2.say(); console.log(p1.__proto__ === p2.__proto__);//true console.log(p1.__proto__); console.log(p2.__proto__);
使用寄生式继承来为对象添加函数,会由于不能做到函数服用而减低效率;这一点与构造函数模式类似;
object();只要传进去的是一个对象,就会返回相同的引用