1.原型链继承
(包含引用类型值的原型属性会被所用实例共享;在创建子类型的实例时,不能向超类型的构造函数中传递参数(实际上,应该说是没有办法在不影响所有对象实例的情况下,给超类型的构造函数传递参数);)
基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。 构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
function Super(){
this.property = true;
}
Super.prototype.getSuperValue = function(){
return this.property;
}
function Sub{
this.subProperty = false;
}
// Sub 继承 Super
Sub.prototype = new Super();
Sub.prototype.getSubValue(){
return this.subProperty;
}
var instance=new Sub();
alert(instance.getSuperValue()); // true
2.借用构造函数
(无法避免构造函数模式存在的问题--方法都在构造函数中定义,因此函数复用就无法谈起了。而且,在超类的原型中定义的方法,对子类而言也是不可见的,结果所有类型都只能使用构造函数模式)
基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。
function Super(){
this.colors=["red","blue","green"];
}
function Sub{
// 继承Super
Super.call(this);
}
var instance1=new Sub();
instance1.colors.push("black");
alert(instance1.colors); // red,blue,green,balck
var instance2=new Sub();
alert(instance2.colors); // red,blue,green
3.组合继承
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。
// 超类
function SuperType(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
if(typeof this.sayName != 'function'){
// SuperType.prototype.sayName = function(){ // 'use strict'
// 'use strict' 严格模式下不能使用 'caller', 'callee', and 'arguments'
arguments.callee.prototype.sayName = function(){
console.log('name = '+this.name);
};
}
}
// 子类
function SubType(name, age){
// 继承了SuperType,同时传递参数
// 继承属性
SuperType.call(this, name); // 借调“超类”构造函数,并传参
// 实例属性
this.age = age;
}
// 继承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance = new SubType("Nicholas",29);
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
var person = {
name:"EvanChen",
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); // "Shelby","Court","Van","Rob","Barbie"
ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。
5.寄生式继承
基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
};
return clone;
}
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi(); // "hi"
6.寄生组合式继承
基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法
function inheritProperty(subType, superType) {
var prototype = object(superType.prototype); // 创建对象
prototype.constructor = subType; // 增强对象
subType.prototype = prototype; // 指定对象
}
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);
this.age = age;
}
inheritProperty(SubType, SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}