成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

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

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

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

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

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

實現(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)點

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

缺點

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

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

實現(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)點(1) 父類原型屬性與方法,子類都能訪問到(2) 實例是子類的實例,也是父類的實例

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

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

實現(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)點(1) 結(jié)合了構(gòu)造+原型鏈繼承的優(yōu)點

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

寄生組合繼承

實現(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);

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

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

額外:借助(Object.create)

實現(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 實現(xiàn)繼承的幾種方式的詳細內(nèi)容,更多關(guān)于JavaScript 實現(xiàn)繼承的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 国产乱子伦片免费观看中字 | 午夜爽 | 成人精品一区二区www | 欧美日本韩国一区 | 欧美亚洲第一区 | 国产精品一区二区资源 | 成人午夜做爰视频免费看 | 欧美日韩成人 | 久久中文字幕久久久久 | 国产高清一区二区三区四区 | 亚洲精品中文字幕久久久久久 | 久久精品免视着国产成人 | 欧美高清一区二区三区欧美 | 国产盗摄视频 | 香港一级特黄高清免费 | 日本一级特黄啪啪片 | 在线观看日韩www视频免费 | 亚洲久久久久 | 久久亚洲精品无码观看不卡 | 亚洲国产成人私人影院 | 免费一级 一片一毛片 | 91成人免费版| 美国毛片在线 | 超级碰碰碰在线观看 | 九九精品99久久久香蕉 | 精品免费国产 | 国产亚洲精品aaa大片 | 欧美日韩免费播放一区二区 | 特黄大片aaaaa毛片 | 美国大毛片 | 日本特级淫片免费 | 国产乱子伦片免费观看中字 | 午夜爽爽爽 | 国产一区二区三区亚洲欧美 | 中文字幕欧美在线观看 | 精品国产亚一区二区三区 | 国产成人久久久精品一区二区三区 | 精品一区二区三区免费毛片爱 | 精品国产_亚洲人成在线高清 | 精品热99| 一级毛片看一个 |