JS繼承實(shí)現(xiàn)方法及優(yōu)缺點(diǎn)詳解
前言
JS作為面向?qū)ο蟮娜躅愋驼Z言,繼承也是其非常強(qiáng)大的特性之一。那么如何在JS中實(shí)現(xiàn)繼承呢?讓我們拭目以待。
JS繼承的實(shí)現(xiàn)方式
既然要實(shí)現(xiàn)繼承,那么首先我們得有一個(gè)父類,代碼如下:
// 定義一個(gè)動(dòng)物類function Animal (name) { // 屬性 this.name = name || ’Animal’; // 實(shí)例方法 this.sleep = function(){ console.log(this.name + ’正在睡覺!’); }}// 原型方法Animal.prototype.eat = function(food) { console.log(this.name + ’正在吃:’ + food);};
1、原型鏈繼承
核心: 將父類的實(shí)例作為子類的原型
function Cat(){ }Cat.prototype = new Animal();Cat.prototype.name = ’cat’;// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.eat(’fish’));console.log(cat.sleep());console.log(cat instanceof Animal); //true console.log(cat instanceof Cat); //true
特點(diǎn):
非常純粹的繼承關(guān)系,實(shí)例是子類的實(shí)例,也是父類的實(shí)例 父類新增原型方法/原型屬性,子類都能訪問到 簡(jiǎn)單,易于實(shí)現(xiàn)缺點(diǎn):
要想為子類新增屬性和方法,必須要在new Animal()這樣的語句之后執(zhí)行,不能放到構(gòu)造器中
無法實(shí)現(xiàn)多繼承
來自原型對(duì)象的所有屬性被所有實(shí)例共享(來自原型對(duì)象的引用屬性是所有實(shí)例共享的)(詳細(xì)請(qǐng)看附錄代碼: 示例1)創(chuàng)建子類實(shí)例時(shí),無法向父類構(gòu)造函數(shù)傳參
推薦指數(shù):★★(3、4兩大致命缺陷)
2017-8-17 10:21:43補(bǔ)充:感謝 MMHS 指出。缺點(diǎn)1中描述有誤:可以在Cat構(gòu)造函數(shù)中,為Cat實(shí)例增加實(shí)例屬性。如果要新增原型屬性和方法,則必須放在new Animal()這樣的語句之后執(zhí)行。
2018-9-10 00:03:45補(bǔ)充:感謝 IRVING_J 指出。缺點(diǎn)3中的描述不夠充分。更正為:來自原型對(duì)象的所有屬性被所有實(shí)例共享。
2、構(gòu)造繼承
核心:使用父類的構(gòu)造函數(shù)來增強(qiáng)子類實(shí)例,等于是復(fù)制父類的實(shí)例屬性給子類(沒用到原型)
function Cat(name){ Animal.call(this); this.name = name || ’Tom’;}// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.sleep());console.log(cat instanceof Animal); // falseconsole.log(cat instanceof Cat); // true
特點(diǎn):
解決了1中,子類實(shí)例共享父類引用屬性的問題 創(chuàng)建子類實(shí)例時(shí),可以向父類傳遞參數(shù) 可以實(shí)現(xiàn)多繼承(call多個(gè)父類對(duì)象)缺點(diǎn):
實(shí)例并不是父類的實(shí)例,只是子類的實(shí)例 只能繼承父類的實(shí)例屬性和方法,不能繼承原型屬性/方法 無法實(shí)現(xiàn)函數(shù)復(fù)用,每個(gè)子類都有父類實(shí)例函數(shù)的副本,影響性能推薦指數(shù):★★(缺點(diǎn)3)
3、實(shí)例繼承
核心:為父類實(shí)例添加新特性,作為子類實(shí)例返回
function Cat(name){ var instance = new Animal(); instance.name = name || ’Tom’; return instance;}// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.sleep());console.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); // false
特點(diǎn):
不限制調(diào)用方式,不管是new 子類()還是子類(),返回的對(duì)象具有相同的效果
缺點(diǎn):
實(shí)例是父類的實(shí)例,不是子類的實(shí)例 不支持多繼承推薦指數(shù):★★
4、拷貝繼承
function Cat(name){ var animal = new Animal(); for(var p in animal){ Cat.prototype[p] = animal[p]; } Cat.prototype.name = name || ’Tom’;}// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.sleep());console.log(cat instanceof Animal); // falseconsole.log(cat instanceof Cat); // true
特點(diǎn):
支持多繼承
缺點(diǎn):
效率較低,內(nèi)存占用高(因?yàn)橐截惛割惖膶傩裕? 無法獲取父類不可枚舉的方法(不可枚舉方法,不能使用for in 訪問到)推薦指數(shù):★(缺點(diǎn)1)
5、組合繼承
核心:通過調(diào)用父類構(gòu)造,繼承父類的屬性并保留傳參的優(yōu)點(diǎn),然后通過將父類實(shí)例作為子類原型,實(shí)現(xiàn)函數(shù)復(fù)用
function Cat(name){ Animal.call(this); this.name = name || ’Tom’;}Cat.prototype = new Animal();// 感謝 @學(xué)無止境c 的提醒,組合繼承也是需要修復(fù)構(gòu)造函數(shù)指向的。Cat.prototype.constructor = Cat;// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.sleep());console.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); // true
特點(diǎn):
彌補(bǔ)了方式2的缺陷,可以繼承實(shí)例屬性/方法,也可以繼承原型屬性/方法 既是子類的實(shí)例,也是父類的實(shí)例 不存在引用屬性共享問題 可傳參 函數(shù)可復(fù)用缺點(diǎn):
調(diào)用了兩次父類構(gòu)造函數(shù),生成了兩份實(shí)例(子類實(shí)例將子類原型上的那份屏蔽了)推薦指數(shù):★★★★(僅僅多消耗了一點(diǎn)內(nèi)存)
6、寄生組合繼承
核心:通過寄生方式,砍掉父類的實(shí)例屬性,這樣,在調(diào)用兩次父類的構(gòu)造的時(shí)候,就不會(huì)初始化兩次實(shí)例方法/屬性,避免的組合繼承的缺點(diǎn)
function Cat(name){ Animal.call(this); this.name = name || ’Tom’;}(function(){ // 創(chuàng)建一個(gè)沒有實(shí)例方法的類 var Super = function(){}; Super.prototype = Animal.prototype; //將實(shí)例作為子類的原型 Cat.prototype = new Super();})();// Test Codevar cat = new Cat();console.log(cat.name);console.log(cat.sleep());console.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); //true感謝 @bluedrink 提醒,該實(shí)現(xiàn)沒有修復(fù)constructor。Cat.prototype.constructor = Cat; // 需要修復(fù)下構(gòu)造函數(shù)
感謝 @bluedrink 提醒,該實(shí)現(xiàn)沒有修復(fù)constructor。
Cat.prototype.constructor = Cat; // 需要修復(fù)下構(gòu)造函數(shù)
特點(diǎn):
堪稱完美
缺點(diǎn):
實(shí)現(xiàn)較為復(fù)雜
推薦指數(shù):★★★★(實(shí)現(xiàn)復(fù)雜,扣掉一顆星)
附錄代碼:
示例一:
function Animal (name) { // 屬性 this.name = name || ’Animal’; // 實(shí)例方法 this.sleep = function(){ console.log(this.name + ’正在睡覺!’); } //實(shí)例引用屬性 this.features = [];}function Cat(name){}Cat.prototype = new Animal();var tom = new Cat(’Tom’);var kissy = new Cat(’Kissy’);console.log(tom.name); // 'Animal'console.log(kissy.name); // 'Animal'console.log(tom.features); // []console.log(kissy.features); // []tom.name = ’Tom-New Name’;tom.features.push(’eat’);//針對(duì)父類實(shí)例值類型成員的更改,不影響console.log(tom.name); // 'Tom-New Name'console.log(kissy.name); // 'Animal'//針對(duì)父類實(shí)例引用類型成員的更改,會(huì)通過影響其他子類實(shí)例console.log(tom.features); // [’eat’]console.log(kissy.features); // [’eat’]
原因分析:
關(guān)鍵點(diǎn):屬性查找過程
執(zhí)行tom.features.push,首先找tom對(duì)象的實(shí)例屬性(找不到),
那么去原型對(duì)象中找,也就是Animal的實(shí)例。發(fā)現(xiàn)有,那么就直接在這個(gè)對(duì)象的features屬性中插入值。
在console.log(kissy.features); 的時(shí)候。同上,kissy實(shí)例上沒有,那么去原型上找。
剛好原型上有,就直接返回,但是注意,這個(gè)原型對(duì)象中features屬性值已經(jīng)變化了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解2. 淺談python出錯(cuò)時(shí)traceback的解讀3. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼4. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解5. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Nginx+php配置文件及原理解析8. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法9. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
