Javascript面向?qū)ο缶幊蹋ǘ簶?gòu)造函數(shù)的繼承

這個(gè)系列的第一部分,主要介紹了如何"封裝"數(shù)據(jù)和方法,以及如何從原型對(duì)象生成實(shí)例。

今天要介紹的是,對(duì)象之間的"繼承"的五種方法。

比如,現(xiàn)在有一個(gè)"動(dòng)物"對(duì)象的構(gòu)造函數(shù)。

function Animal(){

this.species = "動(dòng)物";

}

還有一個(gè)"貓"對(duì)象的構(gòu)造函數(shù)。

function Cat(name,color){

this.name = name;

this.color = color;

}

怎樣才能使"貓"繼承"動(dòng)物"呢?

一、 構(gòu)造函數(shù)綁定

第一種方法也是最簡(jiǎn)單的方法,使用call或apply方法,將父對(duì)象的構(gòu)造函數(shù)綁定在子對(duì)象上,即在子對(duì)象構(gòu)造函數(shù)中加一行:

function Cat(name,color){

Animal.apply(this, arguments);

this.name = name;

this.color = color;

}

var cat1 = new Cat("大毛","黃色");

alert(cat1.species); // 動(dòng)物

二、 prototype模式

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

如果"貓"的prototype對(duì)象,指向一個(gè)Animal的實(shí)例,那么所有"貓"的實(shí)例,就能繼承Animal了。

Cat.prototype = new Animal();

Cat.prototype.constructor = Cat;

var cat1 = new Cat("大毛","黃色");

alert(cat1.species); // 動(dòng)物

代碼的第一行,我們將Cat的prototype對(duì)象指向一個(gè)Animal的實(shí)例。

Cat.prototype = new Animal();

它相當(dāng)于完全刪除了prototype 對(duì)象原先的值,然后賦予一個(gè)新值。但是,第二行又是什么意思呢?

Cat.prototype.constructor = Cat;

原來,任何一個(gè)prototype對(duì)象都有一個(gè)constructor屬性,指向它的構(gòu)造函數(shù)。如果沒有"Cat.prototype = new

Animal();"這一行,Cat.prototype.constructor是指向Cat的;加了這一行以

后,Cat.prototype.constructor指向Animal。

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

更重要的是,每一個(gè)實(shí)例也有一個(gè)constructor屬性,默認(rèn)調(diào)用prototype對(duì)象的constructor屬性。

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

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

alert(cat1.constructor == Animal); // true

這顯然會(huì)導(dǎo)致繼承鏈的紊亂(cat1明明是用構(gòu)造函數(shù)Cat生成的),因此我們必須手動(dòng)糾正,將Cat.prototype對(duì)象的constructor值改為Cat。這就是第二行的意思。

這是很重要的一點(diǎn),編程時(shí)務(wù)必要遵守。下文都遵循這一點(diǎn),即如果替換了prototype對(duì)象,

o.prototype = {};

那么,下一步必然是為新的prototype對(duì)象加上constructor屬性,并將這個(gè)屬性指回原來的構(gòu)造函數(shù)。

o.prototype.constructor = o;

三、 直接繼承prototype

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

現(xiàn)在,我們先將Animal對(duì)象改寫:

function Animal(){ }

Animal.prototype.species = "動(dòng)物";

然后,將Cat的prototype對(duì)象,然后指向Animal的prototype對(duì)象,這樣就完成了繼承。

Cat.prototype = Animal.prototype;

Cat.prototype.constructor = Cat;

var cat1 = new Cat("大毛","黃色");

alert(cat1.species); // 動(dòng)物

與前一種方法相比,這樣做的優(yōu)點(diǎn)是效率比較高(不用執(zhí)行和建立Animal的實(shí)例了),比較省內(nèi)存。缺點(diǎn)是

Cat.prototype和Animal.prototype現(xiàn)在指向了同一個(gè)對(duì)象,那么任何對(duì)Cat.prototype的修改,都會(huì)反映到

Animal.prototype。

所以,上面這一段代碼其實(shí)是有問題的。請(qǐng)看第二行

Cat.prototype.constructor = Cat;

這一句實(shí)際上把Animal.prototype對(duì)象的constructor屬性也改掉了!

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

四、 利用空對(duì)象作為中介

由于"直接繼承prototype"存在上述的缺點(diǎn),所以就有第四種方法,利用一個(gè)空對(duì)象作為中介。

var F = function(){};

F.prototype = Animal.prototype;

Cat.prototype = new F();

Cat.prototype.constructor = Cat;

F是空對(duì)象,所以幾乎不占內(nèi)存。這時(shí),修改Cat的prototype對(duì)象,就不會(huì)影響到Animal的prototype對(duì)象。

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

我們將上面的方法,封裝成一個(gè)函數(shù),便于使用。

function extend(Child, Parent) {

var F = function(){};

F.prototype = Parent.prototype;

Child.prototype = new F();

Child.prototype.constructor = Child;

Child.uber = Parent.prototype;

}

使用的時(shí)候,方法如下

extend(Cat,Animal);

var cat1 = new Cat("大毛","黃色");

alert(cat1.species); // 動(dòng)物

這個(gè)extend函數(shù),就是YUI庫(kù)如何實(shí)現(xiàn)繼承的方法。

另外,說明一點(diǎn),函數(shù)體最后一行

Child.uber = Parent.prototype;

意思是為子對(duì)象設(shè)一個(gè)uber屬性,這個(gè)屬性直接指向父對(duì)象的prototype屬性。(uber是一個(gè)德語(yǔ)詞,意思是"向上"、"上一層"。)這等于在子對(duì)象上打開一條通道,可以直接調(diào)用父對(duì)象的方法。這一行放在這里,只是為了實(shí)現(xiàn)繼承的完備性,純屬備用性質(zhì)。

五、 拷貝繼承

上面是采用prototype對(duì)象,實(shí)現(xiàn)繼承。我們也可以換一種思路,純粹采用"拷貝"方法實(shí)現(xiàn)繼承。簡(jiǎn)單說,如果把父對(duì)象的所有屬性和方法,拷貝進(jìn)子對(duì)象,不也能夠?qū)崿F(xiàn)繼承嗎?這樣我們就有了第五種方法。

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

function Animal(){}

Animal.prototype.species = "動(dòng)物";

然后,再寫一個(gè)函數(shù),實(shí)現(xiàn)屬性拷貝的目的。

function extend2(Child, Parent) {

var p = Parent.prototype;

var c = Child.prototype;

for (var i in p) {

c[i] = p[i];

}

c.uber = p;

}

這個(gè)函數(shù)的作用,就是將父對(duì)象的prototype對(duì)象中的屬性,一一拷貝給Child對(duì)象的prototype對(duì)象。

使用的時(shí)候,這樣寫:

extend2(Cat, Animal);

var cat1 = new Cat("大毛","黃色");

alert(cat1.species); // 動(dòng)物

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容