亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

JavaScript 實(shí)現(xiàn)繼承的幾種方式

瀏覽:57日期:2023-10-04 09:50:08

非ES6代碼實(shí)現(xiàn)繼承的主流方式主要可以分為:構(gòu)造繼承、原型鏈繼承、構(gòu)造繼承+原型鏈繼承組合繼承、以及在組合繼承上衍生出的繼承方式。

構(gòu)造繼承 (借助call實(shí)現(xiàn))

實(shí)現(xiàn)

function Super(age){ this.age = age; this.say = function(){ console.log(this.age) }}function Child(name,age){ Super.call(this,age) this.name = name;}var child = new Child('min',23)console.log(child instanceof Super); // falseconsole.log(child instanceof Child); // true

優(yōu)點(diǎn)

(1) 可以實(shí)現(xiàn)多繼承(call多個(gè)父類(lèi)對(duì)象)(2) 構(gòu)造函數(shù)中可向父級(jí)傳遞參數(shù)

缺點(diǎn)

(1) 只能繼承父類(lèi)實(shí)例的屬性和方法,不能繼承原型上的屬性和方法(2) 實(shí)例并不是父類(lèi)的實(shí)例,只是子類(lèi)的實(shí)例

原型鏈繼承 (借助原型鏈實(shí)現(xiàn))

實(shí)現(xiàn)

function Super(){ this.getName = function(){ console.log(this.name) }}function Child(name){this.name = name;}Child.prototype = new Super(); // 這里可以傳構(gòu)造參數(shù)Child.prototype.constructor = Child;var child = new Child('min');console.log(child instanceof Super); // trueconsole.log(child instanceof Child); // trueconsole.log(child.constructor); // Child

優(yōu)點(diǎn)(1) 父類(lèi)原型屬性與方法,子類(lèi)都能訪問(wèn)到(2) 實(shí)例是子類(lèi)的實(shí)例,也是父類(lèi)的實(shí)例

缺點(diǎn)(1) 無(wú)法實(shí)現(xiàn)多繼承 (2) 創(chuàng)建子類(lèi)實(shí)例時(shí),無(wú)法向父類(lèi)構(gòu)造函數(shù)傳參

組合繼承 (構(gòu)造繼承+原型鏈繼承)

實(shí)現(xiàn)

function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age); }}function Child(name,age){ Super.call(this,age) this.name = name;}Child.prototype = new Super(1); Child.prototype.constructor = Child;var child = new Child('min',23);console.log(child instanceof Super); // trueconsole.log(child instanceof Child); // trueconsole.log(child.constructor); // Child

優(yōu)點(diǎn)(1) 結(jié)合了構(gòu)造+原型鏈繼承的優(yōu)點(diǎn)

缺點(diǎn)(1) Child.prototype = new Super(); 多調(diào)用了一次,使得原型對(duì)象中存在一些不必要屬性,如上面例子中age屬性

寄生組合繼承

實(shí)現(xiàn)

function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age) }}function Child(name,age){ Super.call(this,age) this.name = name;}(function(){ function Copy(){} Copy.prototype = Super.prototype; Child.prototype = new Copy();})()Child.prototype.constructor = Child;var child = new Child('min',23);

備注問(wèn):為什么沒(méi)有直接使用 Child.prototype = Super.prototype;答:Child.prototype.constructor = Child;關(guān)鍵代碼,上面寫(xiě)Super.prototype 也會(huì)變(引用類(lèi)型,指向同一地址)

優(yōu)點(diǎn)(1) 這應(yīng)該是實(shí)現(xiàn)繼承最完美的方案了,es6的extends關(guān)鍵字,在babel轉(zhuǎn)換后代碼也是通過(guò)這種方式實(shí)現(xiàn)的繼承。

額外:借助(Object.create)

實(shí)現(xiàn)

function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age) }}function Child(name,age){ Super.call(this,age) this.name = name;}Child.prototype = Object.create(Super.prototype,{ constructor:{ // 構(gòu)造函數(shù)修復(fù) value: Child }})var child = new Child('min',23);console.log(child instanceof Super); // trueconsole.log(child instanceof Child); // trueconsole.log(child.constructor); // Child

以上就是JavaScript 實(shí)現(xiàn)繼承的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 實(shí)現(xiàn)繼承的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 91视在线国内在线播放酒店 | 最新亚洲手机在线人成网站 | 色老大影院 | 国产精品成人第一区 | a毛片免费观看完整 | 91在线视频在线观看 | 黄视频日本 | 婷婷亚洲五月色综合 | 成人在线网站 | 中国美女一级黄色片 | 国产精品久久精品 | 国产黄色a三级三级三级 | 成人在线观看午夜 | 在线精品亚洲 | 亚洲 国产 路线1路线2路线 | 草草国产成人免费视频 | 久久综合九九 | 黄色 在线 | 日韩精品欧美国产精品亚 | 女教师的一级毛片 | 国产二区三区毛片 | 日本护士69xxxx免费 | 日韩a级片在线观看 | 在线日韩不卡 | 看真人视频一级毛片 | 国产免费自拍 | 欧美激情综合亚洲一二区 | 成人免费久久精品国产片久久影院 | 国产91po在线观看免费观看 | 成人亚洲欧美日韩在线 | 日本香蕉一区二区在线观看 | 亚洲天天在线日亚洲洲精 | jizzjiz熟丰满老妇日本 | 日韩 欧美 亚洲 中文字幕 | 亚洲综合狠狠 | 成人片免费看 | 蕾丝视频在线观看www | 国产美女福利视频福利 | 黑人操中国美女 | 亚洲一在线 | 一级片在线免费 |