理解javascript對(duì)象繼承
先從一個(gè)問題進(jìn)行研究深入,什么是javascript對(duì)象繼承?
比如我們有一個(gè)“動(dòng)物”對(duì)象的構(gòu)造函數(shù)。
function animal() { this.type = '動(dòng)物'; }
還有一個(gè)“貓”對(duì)象的構(gòu)造函數(shù)。
function cat(name,color) { this.name = name; this.color = color; }
我們知道貓也屬于動(dòng)物,如果這個(gè)貓對(duì)象想要繼承動(dòng)物對(duì)象的屬性,我們該怎么做呢?
構(gòu)造函數(shù)綁定
使用構(gòu)造函數(shù)綁定是最簡單的方法,使用call或者apply將父對(duì)象綁定在自對(duì)象上就可以了。
function cat(name,color) { animal.apply(this,arguments); this.name = name; this.color = color; } var cat1 = new cat("haha", 'red'); console.log(cat1.type); //動(dòng)物
不過這種方法比較少見。
拷貝繼承
如果把父對(duì)象的所有屬性和方法,拷貝進(jìn)子對(duì)象,也可以實(shí)現(xiàn)繼承。
function extend(Child, Parent) { var p = Parent.prototype; var c = Child.prototype; for (var i in p) { c[i] = p[i]; } c.uber = p; //橋梁作用 }
使用方法:
extend(cat, animal); var cat1 = new cat("haha","red"); alert(cat1.type); // 動(dòng)物
原型繼承(prototype)
相比于上面的直接綁定,原型繼承的方法比較常見,對(duì)于prototype,我自己簡單總結(jié)了一下。
每個(gè)函數(shù)都有一個(gè)prototype屬性,這個(gè)屬性是指向一個(gè)對(duì)象的引用,當(dāng)使用new關(guān)鍵字創(chuàng)建新實(shí)例的時(shí)候,這個(gè)實(shí)例對(duì)象會(huì)從原型對(duì)象上繼承屬性和方法。
也就是說,如果將“貓”構(gòu)造函數(shù)的prototype屬性指向一個(gè)“動(dòng)物”實(shí)例,那么再創(chuàng)建“貓”對(duì)象實(shí)例的時(shí)候,就繼承了“動(dòng)物”對(duì)象的屬性和方法了。
繼承實(shí)例
cat.prototype = new animal(); cat.prototype.constructor = cat; var cat1 = new cat("haha","red"); console.log(cat1.constructor == cat); //true console.log(cat1.type); // 動(dòng)物
1、代碼第一行,我們將cat函數(shù)的prototype對(duì)象指向一個(gè)animal對(duì)象的實(shí)例(其中就包含了animal的type屬性了)。
2、代碼第二行是什么意思呢?
1)、首先,假如我們沒有加這行代碼,運(yùn)行
cat.prototype = new animal();
console.log(cat.prototype.constructor == animal); //true
也就是說,其實(shí)每個(gè)prototype對(duì)象都有一個(gè)constructor屬性,指向它的構(gòu)造函數(shù)。
2)、我們再看下面的代碼
cat.prototype = new animal(); var cat1 = new cat("haha", 'red'); console.log(cat1.constructor == animal); //true
由上我們看到實(shí)例cat1的構(gòu)造函數(shù)是animal,所以,顯然是不對(duì)的。。。cat1明明是new cat()才生成的,所以我們應(yīng)該手動(dòng)糾正。cat.prototype對(duì)象的constructor值改為cat。
3)、所以這也是我們應(yīng)該注意的一點(diǎn),如果我們替換了prototype對(duì)象,就應(yīng)該手動(dòng)糾正prototype對(duì)象的constructor屬性。
o.prototype = {};
o.prototype.constructor = o;
直接繼承prototype
由于在animal對(duì)象中,不變的屬性可以直接寫在animal.prototype中。然后直接讓cat.prototype指向animal.prototype也就實(shí)現(xiàn)了繼承。
現(xiàn)在我們先將animal對(duì)象改寫成:
function animal() { } animal.prototype.type = '動(dòng)物';
然后再實(shí)現(xiàn)繼承:
cat.prototype = animal.prototype; cat.prototype.constructor = cat; var cat1 = new cat("haha","red"); console.log(cat1.type); // 動(dòng)物
與上一種方法相比,這種方法顯得效率更高(沒有創(chuàng)建animal實(shí)例),節(jié)省了空間。但是這樣做正確嗎?答案是不正確,我們繼續(xù)看。
cat.prototype = animal.prototype;
這行代碼讓cat.prototype和animal.prototype指向了同一個(gè)對(duì)象,所以如果改變了cat.prototype的某一個(gè)屬性,都會(huì)反映到animal.prototype上,這顯然不是我們想要看到的。
比如我們運(yùn)行:
console.log(animal.prototype.constructor == animal) //false
結(jié)果看到是false,為什么呢?cat.prototype.constructor = cat;這一行就會(huì)把a(bǔ)nimal.prototype的constructor屬性也改掉了。
利用空對(duì)象作為中介
var F = function(){}; F.prototype = animal.prototype; cat.prototype = new F(); cat.prototype.constructor = cat;
結(jié)合上面兩種方法,因?yàn)镕是空對(duì)象,所以幾乎不占內(nèi)存。這時(shí)修改cat的prototype對(duì)象,就不會(huì)影響到animal的prototype對(duì)象。
console.log(animal.prototype.constructor == animal); // true
然后我們將上面的方法封裝一下:
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("haha","red"); console.log(cat1.type); // 動(dòng)物
Child.uber = Parent.prototype; 這行代碼就是個(gè)橋梁作用,讓子對(duì)象的uber屬性直接指向父對(duì)象的prototype屬性,等于在自對(duì)象上打開一條叫uber的通道,讓子對(duì)象的實(shí)例能夠使用父對(duì)象的所有屬性和方法。
以上就是對(duì)javascript對(duì)象繼承我的理解,希望或多或少能夠幫助到大家,謝謝大家的閱讀。
相關(guān)文章
用js控件div的滾動(dòng)條,讓它在內(nèi)容更新時(shí)自動(dòng)滾到底部的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄胘s控件div的滾動(dòng)條,讓它在內(nèi)容更新時(shí)自動(dòng)滾到底部的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10javascript超過容器后顯示省略號(hào)效果的方法(兼容一行或者多行)
下面小編就為大家?guī)硪黄猨avascript超過容器后顯示省略號(hào)效果的方法(兼容一行或者多行)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07JS組件Bootstrap實(shí)現(xiàn)彈出框效果代碼
這篇文章主要介紹了JS組件Bootstrap實(shí)現(xiàn)彈出框效果代碼的相關(guān)資料,對(duì)彈出框感興趣的小伙伴們可以參考一下2016-04-04Javascript與vbscript數(shù)據(jù)共享
Javascript與vbscript數(shù)據(jù)共享...2007-01-01js 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡單實(shí)現(xiàn))
下面小編就為大家?guī)硪黄猨s 基礎(chǔ)篇必看(點(diǎn)擊事件輪播圖的簡單實(shí)現(xiàn))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08