olas";
instance1.sayAge(); //29
var instance2 = new SubType("Greg", 27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27
可以看到,对于需要共享的属性、方法,采用原型链的方式继承,对于不需要的共享的,比如属性,则用call方法实现继承。这种方式是java script中最常用的继承方式。 原型式继承 这个方式是json格式的发明人Douglas Crockford所创造。 提出了一个object()函数,可以做到这一点。
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
这个object()函数,借用了一个临时函数,把传入的对象作为父对象,作为临时函数的prototype属性,并返回这个临时函数的一个实例作为子对象,从而使得子对象与父对象连在一起。
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");
?
alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
这个例子中,person对象作为父对象,把它传入到object函数中,会返回一个新的对象,该对象的原型就是person,所以它的原型中就包含一个基本类型值属性和一个引用类型值属性。这意味着person.friends 不仅属于person 所有,而且也会被anotherPerson以及yetAnotherPerson 共享。实际上,这就相当于又创建了person 对象的两个副本。
参考资料: