javascript的幾種繼承方法介紹

字號(hào):


    下面小編就為大家?guī)硪黄猨avascript的幾種繼承方法介紹。小編覺得挺不錯(cuò)的?,F(xiàn)在分享給大家,給大家一個(gè)參考
    1.原型鏈繼承:構(gòu)造函數(shù)、原型和實(shí)例的關(guān)系:每個(gè)構(gòu)造函數(shù)都有一個(gè)原型對(duì)象,原型對(duì)象都包含一個(gè)指向構(gòu)造函數(shù)的指針,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針。確認(rèn)原型和實(shí)例之間的關(guān)系用instanceof。
    原型鏈繼承缺點(diǎn):字面量重寫原型會(huì)中斷關(guān)系,使用引用類型的原型,并且子類型還無(wú)法給超類型傳遞參數(shù)
    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);//得到被繼承的屬性
      //孫子繼續(xù)原型鏈繼承兒子
      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.構(gòu)造函數(shù)實(shí)現(xiàn)繼承:又叫偽造對(duì)象或經(jīng)典繼承。
    構(gòu)造函數(shù)實(shí)現(xiàn)繼承缺點(diǎn):借用構(gòu)造函數(shù)雖然解決了原型鏈繼承的兩種問題,但沒有原型,則復(fù)用無(wú)從談起,所以需要原型鏈+借用構(gòu)造函數(shù)模式。
    function Parent(age){
        this.name=['mike','jack','smith'];
        this.age=age;
      }
      function Child(age){
        Parent.call(this,age);//把this指向Parent,同時(shí)還可以傳遞參數(shù)
      }
      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.組合繼承:使用原型鏈實(shí)現(xiàn)對(duì)原型屬性和方法的繼承,而通過借用構(gòu)造函數(shù)來實(shí)現(xiàn)對(duì)實(shí)例屬性的繼承。這樣即通過在原型上定義方法實(shí)現(xiàn)了函數(shù)復(fù)用,又保證每個(gè)實(shí)現(xiàn)都有它自己的屬性。
    缺點(diǎn):無(wú)論什么情況下,都會(huì)調(diào)用兩次超類型構(gòu)造函數(shù),一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在子類型構(gòu)造函數(shù)內(nèi)部。
    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);//給超類型傳參,第二次調(diào)用
      }
      Child.prototype=new Parent();//原型鏈繼承,第一次調(diào)用
      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同時(shí)又可以有run方法
    4.原型式繼承:借助原型可以基于已有的對(duì)象創(chuàng)建新對(duì)象,同時(shí)還不必因此創(chuàng)建自定義類型。它要求必須有一個(gè)對(duì)象可以作為另一個(gè)對(duì)象的基礎(chǔ)。
    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()方法規(guī)范化了原型式繼承,這個(gè)方法接收兩個(gè)參數(shù):一個(gè)用作新對(duì)象原型的對(duì)象和(可選的)一個(gè)為新對(duì)象定義屬性的對(duì)象。
    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"]
      /*以這種方式指定的任何屬性都會(huì)覆蓋原型對(duì)象上的同名屬性。*/
      var threeP=Object.create(person,{
        name:{value:'red'}
      });
      console.log(threeP.name);//red,如果threeP中無(wú)name則輸出person2里的name值nicho
    5.寄生式繼承:思路與寄生構(gòu)造函數(shù)和工廠模式類似,即創(chuàng)建一個(gè)僅用于封裝繼承過程的函數(shù),該函數(shù)在內(nèi)部以某種方式來增強(qiáng)對(duì)象,最后再像真地是它做了所有工作一樣返回對(duì)象。
    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"]
      /*這個(gè)例子中的代碼基于 person 返回了一個(gè)新對(duì)象—— anotherPerson 。 新對(duì)象不僅具有 person
       的所有屬性和方法,而且還有自己的 sayHi() 方法*/
    寄生組合式繼承:無(wú)論什么情況下,都會(huì)調(diào)用兩次超類型構(gòu)造函數(shù),一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在子類型構(gòu)造函數(shù)內(nèi)部,這樣子類型最終會(huì)包含超類型對(duì)象的全部實(shí)例屬性,我們不得不在調(diào)用子類型構(gòu)造函數(shù)時(shí)重寫這些屬性。因此出現(xiàn)了寄生組合式繼承。
    6.寄生組合式繼承:借用構(gòu)造函數(shù)來繼承屬性,通過原型鏈的混成形式來繼承方法?;舅悸罚翰槐貫榱酥付ㄗ宇愋偷脑投{(diào)用超類型的構(gòu)造函數(shù)。本質(zhì)上就是使用寄生式繼承來繼承超類型的原型,然后再將結(jié)果指定給子類型的原型。
    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此函數(shù)第一步是創(chuàng)建超類型原型的一個(gè)副本。第二步是為創(chuàng)建的副本添加constructor屬性,
      * 從而彌補(bǔ)因重寫原型而失去的默認(rèn)的constructor屬性,第三步將新創(chuàng)建的對(duì)象(副本)賦值給子類型的原型*/
      function inheritPrototype(subType,superType){
        var prototype=object(superType.prototype);//創(chuàng)建對(duì)象
        prototype.constructor=subType;//增強(qiáng)對(duì)象
        subType.prototype=prototype;//指定對(duì)象
      }
      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)
    此方法優(yōu)點(diǎn):只調(diào)用了一次父類SuperType構(gòu)造函數(shù),并且因此避免了在SubType.prototype上面創(chuàng)建不必要的多余的屬性。同時(shí)原型鏈還能保持不變,還能正常使用instanceof和isPrototypeOf();
    以上這篇javascript的幾種繼承方法介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考