function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承了 SuperType ,重写了SubType.prototype,此时SubType.prototype的constructor属性已经不指向构造函数Subtype了,二而是作为SuperType的实例指向SuperType.prototype
SubType.prototype = new SuperType();
//在继承了 SuperType 的属性和方 法的基础上又添加了一个新方法
SubType.prototype.getSubValue = function (){
return this.subproperty;
}
var instance = new SubType();
alert(instance.getSuperValue());//true
重写原型对象
,代之以一个新类型的实例。换句话说,原来存在于 SuperType 的实例中的所有属性和方法,现在也存在于 SubType.prototype 中了。以下是上面例子中的实例以及构造函数和原型之间的关系如图:有两种方式可以确定原型和实例之间的关系:第一种方式是使用 instanceof 操作符
,第二种方式是使用 isPrototypeOf()方法
。
alert(instance instanceof Object); //true
alert(instance instanceof SuperType); //true
alert(instance instanceof SubType); //true
alert(Object.prototype.isPrototypeOf(instance)); //true
alert(SuperType.prototype.isPrototypeOf(instance)); //true
alert(SubType.prototype.isPrototypeOf(instance)); //true
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
};
function SubType(){
this.subproperty = false;
}
//继承了 SuperType 先替换原型
SubType.prototype = new SuperType();
//添加新方法 后添加方法
SubType.prototype.getSubValue = function (){
return this.subproperty;
};
//重写超类型中的方法 【并不是修改了超类型(SuperType.prototype)中的方法,而是在SubType.prototype中添加了一个方法】
SubType.prototype.getSuperValue = function (){
return false;
};
var instance = new SubType();
console.log(instance.getSuperValue()); //false
//通过SuperType 的实例调用 getSuperValue()时,还会继续调用原来的那个方法。即原来位置上的方法并没有被改写或替换,只是被屏蔽了。
var instance1 = new SuperType();
console.log(instance1.getSuperValue());
注意:给一个对象添加一个自身并不具有的属性,而原型却具有同名的属性,那么结果不是改变原型对象上的属性,而是给这个对象自身添加了这个同名属性!原型上的该属性,并不会改变!只是在访问实例的时候被屏蔽!
function SuperType(){
this.colors = ["red", "blue", "green"];
}
function SubType(){
}
//继承了 SuperType
SubType.prototype = new SuperType();
var instance1 = new SubType();
instance1.colors.push("black"); //修改的是SubType原型上的colors属性
alert(instance1.colors); //"red,blue,green,black"
var instance2 = new SubType();
alert(instance2.colors); //"red,blue,green,black"
apply()
和call()
方法。SubType 构造 函数
内部调用 SuperType 构造函数
时,实际上是为 SubType 的实例设置了 name 属性。如果仅仅是借用构造函数,那么也将无法避免构造函数模式存在的问题——方法都在构造函数中定 义,因此函数复用就无从谈起了。
function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
}
function SubType(name, age){
//继承属性
SuperType.call(this, name); //包括继承name和color属性
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
}
//每创建一个SubType实例,就调用一次SuperType构造函数,修改的是创建的当前实例下的colors属性。
var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
console.log(instance1);
instance1.sayName();//"Nicholas";
instance1.sayAge();//29
var instance2 = new SubType("Greg", 27);
console.log(instance2.colors);//"red,blue,green"
instance2.sayName();//"Greg";
instance2.sayAge();//27
instanceof 和 isPrototypeOf()
也能够用于识别基于组合继承创建的对象。 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";//在实例中 添加属性name
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
Object.create()
方法:ECMAScript 5 通过新增 Object.create()方法规范化了原型式继承。【用Object.create()就不用提前定义函数object()了】
var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
//包含引用类型值的属性始终都会共享相应的值就像使用原型模式一样。
alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
Object.create()
方法的第二个参数与 Object.defineProperties()方法
的第二个参数格式相同:每个属性都是通过自己的描述符定义的。以这种方式指定的任何属性都会覆盖原型对象上的同名属性。例如: var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
};
var anotherPerson = Object.create(person, {
name: {
value: "Greg"
}
});
alert(anotherPerson.name); //"Greg"
前面说过,组合继承是 JavaScript 最常用的继承模式;不过,它也有自己的不足。组合继承最大的问题就是无论什么情况下,都会调用两次超类型构造函数:一次是在创建子类型原型的时候,另一次是 在子类型构造函数内部。
所谓寄生组合式继承,即通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。其背后的基本思路是:不必为了指定子类型的原型而调用超类型的构造函数,我们所需要的无非就是超类型原型的一个副本而已。本质上,就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型 的原型。寄生组合式继承的基本模式如下所示。
这个示例中的 inheritPrototype()函数
实现了寄生组合式继承的最简单形式。这个函数接收两个参数:子类型构造函数和超类型构造函数。在函数内部,第一步是创建超类型原型的一个副本。第二步是为创建的副本添加 constructor 属性,从而弥补因重写原型而失去的默认的 constructor 属性。 最后一步,将新创建的对象(即副本)赋值给子类型的原型。
理解难点:指的是第三步,将prototype赋值给subType的原型,由于创建的副本(superType.prototype的实例)中没有constructor属性,为了使subType.prototype中的constructor属性不变,所以要提前在第二部添加constructor属性。用这样的方式将父类型的实例赋值给子类型的原型,但却不用再调用一次父类型构造函数
这样,我们就可以用调用 inherit- Prototype()函数的语句,去替换前面例子中为子类型原型赋值的语句了,例如: