1.原型鏈繼承:建構子、原型與實例的關係:每個建構子都有一個原型對象,原型物件都包含一個指向建構函式的指針,而實例都包含一個指向原型物件的內部指標。確認原型和實例之間的關係用instanceof。
原型鏈繼承缺點:字面量重寫原型會中斷關係,使用引用類型的原型,並且子類型還無法給超類型傳遞參數
function Parent(){ this.name='mike'; } function Child(){ this.age=12; } //儿子继承父亲(原型链) Child.prototype=new Parent();//Child继承Parent,通过原型形成链条 var test=new Child(); console.log(test.age); console.log(test.name);//得到被继承的属性 //孙子继续原型链继承儿子 function Brother(){ this.weight=60; } Brother.prototype=new Child();//继承原型链继承 var brother=new Brother(); console.log(brother.name);//继承了Parent和Child,弹出mike console.log(brother.age);//12 console.log(brother instanceof Child);//ture console.log(brother instanceof Parent);//ture console.log(brother instanceof Object);//ture
2.建構子實作繼承:又叫偽造物件或經典繼承。
建構函式實作繼承缺點:借用建構函式雖然解決了原型鏈繼承的兩種問題,但沒有原型,則複用無從談起,所以需要原型鏈+借用建構函式模式。
function Parent(age){ this.name=['mike','jack','smith']; this.age=age; } function Child(age){ Parent.call(this,age);//把this指向Parent,同时还可以传递参数 } var test=new Child(21); console.log(test.age);//21 console.log(test.name); test.name.push('bill'); console.log(test.name);//mike,jack,smith,bill
3.組合繼承:使用原型鏈實現原型屬性和方法的繼承,而藉由借用建構函式來實現實例屬性的繼承。這樣即透過在原型上定義方法實現了函數復用,並保證每個實作都有它自己的屬性。
缺點:無論什麼情況下,都會呼叫兩次超類型構造函數,一次是在創建子類型原型的時候,另一次是在創建子類型原型的時候,另一次是在子類型構造函數內部。
function Parent(age){ this.name=['mike','jack','smith']; this.age=age; } Parent.prototype.run=function(){ return this.name+' are both '+this.age; } function Child(age){ Parent.call(this,age);//给超类型传参,第二次调用 } Child.prototype=new Parent();//原型链继承,第一次调用 var test1=new Child(21);//写new Parent(21)也行 console.log(test1.run());//mike,jack,smith are both 21 var test2=new Child(22); console.log(test2.age); console.log(test1.age); console.log(test2.run()); //这样可以使test1和test2分别拥有自己的属性age同时又可以有run方法
4.原型式繼承:借助原型可以基於已有的對象創建新對象,同時還不必因此創建自定義類型。它要求必須有一個物件可以作為另一個物件的基礎。
function object(o){ function F(){}; F.prototype=o; return new F(); } var person={ name:'nicho', friends:['shell','jim','lucy'] } var anotherPerson = object(person); anotherPerson.name = 'Greg'; anotherPerson.friends.push('Rob'); console.log(anotherPerson.friends);//["shell", "jim", "lucy", "Rob"] var yetAnotherPerson = object(person); yetAnotherPerson.name = 'Linda'; yetAnotherPerson.friends.push('Barbie'); console.log(yetAnotherPerson.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] console.log(person.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
ECMAScript5透過新增Object.create()方法規範化了原型式繼承,這個方法接收兩個參數:一個用作新物件原型的物件和(可選的)一個為新物件定義屬性的物件。
var person2={ name:'nicho', friends:['shell','jim','lucy'] }; var anoP2=Object.create(person2); anoP2.name="Greg"; anoP2.friends.push('Rob'); console.log(anoP2.friends);//["shell", "jim", "lucy", "Rob"] var yetP2=Object.create(person2); yetP2.name="Linda"; yetP2.friends.push('Barbie'); console.log(yetP2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] console.log(person2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"] /*以这种方式指定的任何属性都会覆盖原型对象上的同名属性。*/ var threeP=Object.create(person,{ name:{value:'red'} }); console.log(threeP.name);//red,如果threeP中无name则输出person2里的name值nicho
5.寄生式繼承:思路與寄生構造函數和工廠模式類似,即創建一個僅用於封裝繼承過程的函數,該函數在內部以某種方式來增強對象,最後再像真地是它做了所有工作一樣返回對象。
function object(o){ function F(){}; F.prototype=o; return new F(); }; function createAnother(o){ var cl=object(o); cl.sayHi=function(){ console.log('hi'); } return cl; }; var person={ name:'nick', friends:['shelby','court','van'] } var anotherPerson=createAnother(person); anotherPerson.sayHi();//hi console.log(anotherPerson.name);//nick console.log(anotherPerson.friends);//["shelby", "court", "van"] /*这个例子中的代码基于 person 返回了一个新对象—— anotherPerson 。 新对象不仅具有 person 的所有属性和方法,而且还有自己的 sayHi() 方法*/
寄生組合式繼承:無論什麼情況下,都會呼叫兩次超類型構造函數,一次是在創建子類型原型的時候,另一次是在創建子類型原型的時候,另一次是在子類型構造函數內部,這樣子類型最終會包含超類型物件的全部實例屬性,我們不得不在呼叫子類型建構函數時重寫這些屬性。因此出現了寄生組合式繼承。
6.寄生組合式繼承:借用建構子來繼承屬性,透過原型鏈的混成形式來繼承方法。基本想法:不必為了指定子類型的原型而呼叫超類型的建構子。本質上就是使用寄生式繼承來繼承超類型的原型,然後再將結果指定給子類型的原型。
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; } function object(o){ function F(){}; F.prototype=o; return new F(); }; /*inheritPrototype此函数第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor属性, * 从而弥补因重写原型而失去的默认的constructor属性,第三步将新创建的对象(副本)赋值给子类型的原型*/ function inheritPrototype(subType,superType){ var prototype=object(superType.prototype);//创建对象 prototype.constructor=subType;//增强对象 subType.prototype=prototype;//指定对象 } inheritPrototype(SubType,SuperType); SubType.prototype.sayAge=function(){ console.log(this.age); } var p=new SubType('xiaoli',24); console.log(p.sayName()); console.log(p.sayAge()); console.log(p.colors)
此方法優點:只呼叫了一次父類別SuperType建構函數,並且因此避免了在SubType.prototype上面建立不必要的多餘的屬性。同時原型鏈還能維持不變,還能正常使用instanceof和isPrototypeOf();
以上這篇javascript的幾種繼承方法介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。