大多OOP语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中没法实现接口继承,ECMAScript只支持实现继承,并且其实现继承主要是依靠 原型链 来实现。javascript
1.原型链java
基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法。app
构造函数,原型,实例之间的关系:每一个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。函数
原型链实现继承例子:this
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; } function subType() { this.property = false; } //继承了SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function (){ return this.property; } var instance = new SubType(); console.log(instance.getSuperValue());//true
2.借用构造函数spa
基本思想:在子类型构造函数的内部调用超类构造函数,经过使用call()和apply()方法能够在新建立的对象上执行构造函数。prototype
例子:指针
function SuperType() { this.colors = ["red","blue","green"]; } function SubType() { SuperType.call(this);//继承了SuperType } var instance1 = new SubType(); instance1.colors.push("black"); console.log(instance1.colors);//"red","blue","green","black" var instance2 = new SubType(); console.log(instance2.colors);//"red","blue","green"
3.组合继承对象
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥二者之长的一种继承模式。blog
例子:
function SuperType(name) { this.name = name; this.colors = ["red","blue","green"]; } SuperType.prototype.sayName = function() { console.log(this.name); } function SubType(name, age) { SuperType.call(this,name);//继承属性 this.age = age; } //继承方法 SubType.prototype = new SuperType(); Subtype.prototype.constructor = Subtype; Subtype.prototype.sayAge = function() { console.log(this.age); } var instance1 = new SubType("EvanChen",18); instance1.colors.push("black"); consol.log(instance1.colors);//"red","blue","green","black" instance1.sayName();//"EvanChen" instance1.sayAge();//18 var instance2 = new SubType("EvanChen666",20); console.log(instance2.colors);//"red","blue","green" instance2.sayName();//"EvanChen666" instance2.sayAge();//20
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"
Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用做新对象原型的对象和一个做为新对象定义额外属性的对象。
var person = { name:"EvanChen", 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"); console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
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); }
以上为六种JavaScript的继承方式。