javascript的幾種繼承方法介紹,javascript幾種繼承

來源:互聯網
上載者:User

javascript的幾種繼承方法介紹,javascript幾種繼承

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的幾種繼承方法介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.