JS繼承的幾種方法總結

Sharron40F 7年前發布 | 11K 次閱讀 JavaScript開發 JavaScript

  • 由于js不像java那樣是真正面向對象的語言,js是基于對象的,它沒有類的概念
  • 所以,要想實現繼承,可以用js的原型prototype機制或者用apply和call方法去實現。在面向對象的語言中,我們使用類來創建一個自定義對象。然而js中所有事物都是對象,那么用什么辦法來創建自定義對象呢?
  • 這就需要用到js的原型:我們可以簡單的把prototype看做是一個模版,新創建的自定義對象都是這個模版(prototype)的一個拷貝 (實際上不是拷貝而是鏈接,只不過這種鏈接是不可見,新實例化的對象內部有一個看不見的 Proto 指針,指向原型對象)。

1、繼承第一種方式:對象冒充

blogfunction Parent(username){
    this.username = username;
    this.hello = function(){
      console.log('hello ' + this.username);
    }
  }
Parent.prototype.sayMorning = function(){
    console.log('good morning ' + this.username);
}
  function Child(username,password){
    //通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承
    //第一步:this.method是作為一個臨時的屬性,并且指向Parent所指向的對象,
    //第二步:執行this.method方法,即執行Parent所指向的對象函數
    //第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法
    this.method = Parent;
    this.method(username);//最關鍵的一行
    delete this.method;

 this.password = password;
this.world = function(){
  console.log(this.password);
}

}

var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); parent.sayMorning(); child.hello(); child.world(); </code></pre>

2、繼承第二種方式:call()方法方式

function Parent(username){
    this.username = username;
    this.hello = function(){
      console.log(this.username);
    }
  }

Parent.prototype.sayMorning = function(){ console.log('good morning ' + this.username); }

function Child(username,password){ Parent.call(this,username);

this.password = password;
this.world = function(){
  console.log(this.password);
}

}

var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); parent.sayMorning(); child.hello(); child.world(); // child.sayMorning(); 通過prototype 添加的方法和屬性,不能用來繼承 </code></pre>

3、繼承的第三種方式:apply()方法方式

function Parent(username){
    this.username = username;
    this.hello = function(){
      console.log(this.username);
    }
  }

Parent.prototype.sayMorning = function(){
    console.log('good morning ' + this.username);
}

function Child(username,password){ Parent.apply(this,new Array(username));

this.password = password;
this.world = function(){
  console.log(this.password);
}

} var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); parent.sayMorning(); child.hello(); child.world(); // child.sayMorning(); 通過prototype 添加的方法和屬性,不能用來繼承 </code></pre>

4、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承

function Person(){
  }
  Person.prototype.hello = "hello";
  Person.prototype.sayHello = function(){
    console.log(this.hello);
  }

function Child(){ } Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承 Child.prototype.world = "world"; Child.prototype.sayWorld = function(){ console.log(this.world); }

var c = new Child(); c.sayHello(); c.sayWorld(); 通過prototype 添加的方法和屬性,不能用來繼承 </code></pre>

5、繼承的第五種方式:混合方式, 混合了call或者apply方式、原型鏈方式

function Parent(hello){
    this.hello = hello;
  }
  Parent.prototype.sayHello = function(){
    console.log(this.hello);
  }

function Child(hello,world){ Parent.call(this,hello);//將父類的屬性繼承過來 this.world = world;//新增一些屬性 }

Child.prototype = new Parent();//將父類的方法繼承過來

Child.prototype.sayWorld = function(){//新增一些方法 console.log(this.world); }

var c = new Child("zhangsan","lisi"); c.sayHello(); c.sayWorld(); 可以繼承通過prototype 添加的方法和屬性 </code></pre>

 

來自:http://blog.poetries.top/2016/12/13/js-inherit/

 

 本文由用戶 Sharron40F 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!