Javascript 寄生组合式继承
接触了好几种继承模式了,在前一篇文章,我们就说到了一些继承方法,本以为组合是最好的,结果没想到还有。
故事从原型式继承说起:
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);