JavaScript裡物件導向的繼承:建構函式"繼承"的五種方法

來源:互聯網
上載者:User

標籤:黃色   on()   一個   必須   javascrip   因此   ext   存在   如何   

//現在有一個"動物"對象的建構函式。function Animal(){    this.species = "動物";}//還有一個"貓"對象的建構函式。function Cat(name,color){    this.name = name;    this.color = color;}

  怎樣才能使"貓"繼承"動物"呢?

一、 建構函式綁定

  第一種方法也是最簡單的方法,使用call或apply方法,將父物件的建構函式綁定在子物件上,即在子物件建構函式中加一行:

  function Cat(name,color){    Animal.apply(this, arguments);    this.name = name;    this.color = color;  }  var cat1 = new Cat("大毛","黃色");  alert(cat1.species); // 動物

二、 prototype模式

  第二種方法更常見,使用prototype屬性。

  如果"貓"的prototype對象,指向一個Animal的執行個體,那麼所有"貓"的執行個體,就能繼承Animal了。

Cat.prototype = new Animal();Cat.prototype.constructor = Cat;var cat1 = new Cat("大毛","黃色");alert(cat1.species); // 動物

  代碼的第一行,我們將Cat的prototype對象指向一個Animal的執行個體。Cat.prototype = new Animal();它相當於完全刪除了prototype 對象原先的值,然後賦予一個新值。

  但是,第二行又是什麼意思呢?Cat.prototype.constructor = Cat;

  原來,任何一個prototype對象都有一個constructor屬性,指向它的建構函式。如果沒有"Cat.prototype = new Animal();"這一行,Cat.prototype.constructor是指向Cat的;加了這一行以後,Cat.prototype.constructor指向Animal。

alert(Cat.prototype.constructor == Animal); //true

  更重要的是,每一個執行個體也有一個constructor屬性,預設調用prototype對象的constructor屬性。

  因此,在運行"Cat.prototype = new Animal();"這一行之後,cat1.constructor也指向Animal!

alert(cat1.constructor == Cat.prototype.constructor); // truealert(cat1.constructor == Animal); // true

  這顯然會導致繼承鏈的紊亂(cat1明明是用建構函式Cat產生的),因此我們必須手動糾正,將Cat.prototype對象的constructor值改為Cat。這就是第二行的意思。

  這是很重要的一點,編程時務必要遵守。下文都遵循這一點,即如果替換了prototype對象,那麼,下一步必然是為新的prototype對象加上constructor屬性,並將這個屬性指回原來的建構函式

o.prototype = {};o.prototype.constructor = o;

三、 直接繼承prototype

  第三種方法是對第二種方法的改進。由於Animal對象中,不變的屬性都可以直接寫入Animal.prototype。所以,我們也可以讓Cat()跳過 Animal(),直接繼承Animal.prototype。

  現在,我們先將Animal對象改寫:

function Animal(){ }Animal.prototype.species = "動物";

  然後,將Cat的prototype對象,然後指向Animal的prototype對象,這樣就完成了繼承。

Cat.prototype = Animal.prototype;Cat.prototype.constructor = Cat;var cat1 = new Cat("大毛","黃色");alert(cat1.species); // 動物

  與前一種方法相比,這樣做的優點是效率比較高(不用執行和建立Animal的執行個體了),比較省記憶體。

  缺點是 Cat.prototype和Animal.prototype現在指向了同一個對象,那麼任何對Cat.prototype的修改,都會反映到Animal.prototype。

  所以,上面這一段代碼其實是有問題的。請看第二行

Cat.prototype.constructor = Cat;//這一句實際上把Animal.prototype對象的constructor屬性也改掉了!alert(Animal.prototype.constructor); // Cat

四、 利用Null 物件作為中介

  由於"直接繼承prototype"存在上述的缺點,所以就有第四種方法,利用一個Null 物件作為中介。

var F = function(){};F.prototype = Animal.prototype;Cat.prototype = new F();Cat.prototype.constructor = Cat;

  F是Null 物件,所以幾乎不佔記憶體。這時,修改Cat的prototype對象,就不會影響到Animal的prototype對象

alert(Animal.prototype.constructor); // Animal

  我們將上面的方法,封裝成一個函數,便於使用。

  function extend(Child, Parent) {    var F = function(){};    F.prototype = Parent.prototype;    Child.prototype = new F();    Child.prototype.constructor = Child;    Child.uber = Parent.prototype;  }
//使用的時候,方法如下extend(Cat,Animal);var cat1 = new Cat("大毛","黃色");alert(cat1.species); // 動物

  這個extend函數,就是YUI庫如何?繼承的方法。

  另外,說明一點,函數體最後一行:Child.uber = Parent.prototype; 意思是為子物件設一個uber屬性,這個屬性直接指向父物件的prototype屬性。(uber是一個德語詞,意思是"向上"、"上一層"。)這等於在子物件上開啟一條通道,可以直接調用父物件的方法。這一行放在這裡,只是為了實現繼承的完備性,純屬備用性質。

五、 拷貝繼承

  上面是採用prototype對象,實現繼承。我們也可以換一種思路,純粹採用"拷貝"方法實現繼承。簡單說,如果把父物件的所有屬性和方法,拷貝進子物件,不也能夠實現繼承嗎?這樣我們就有了第五種方法。

  首先,還是把Animal的所有不變屬性,都放到它的prototype對象上。

function Animal(){}Animal.prototype.species = "動物";

  然後,再寫一個函數,實現屬性拷貝的目的。

    function extend2(Child, Parent) {    var p = Parent.prototype;    var c = Child.prototype;    for (var i in p) {      c[i] = p[i];    }    c.uber = p;  } 

  這個函數的作用,就是將父物件的prototype對象中的屬性,一一拷貝給Child對象的prototype對象。

  使用的時候,這樣寫:

extend2(Cat, Animal);var cat1 = new Cat("大毛","黃色");alert(cat1.species); // 動物

 

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.