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

您的位置:首頁技術文章
文章詳情頁

JavaScript 實現繼承的幾種方式

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

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

構造繼承 (借助call實現)

實現

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

優點

(1) 可以實現多繼承(call多個父類對象)(2) 構造函數中可向父級傳遞參數

缺點

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

原型鏈繼承 (借助原型鏈實現)

實現

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

優點(1) 父類原型屬性與方法,子類都能訪問到(2) 實例是子類的實例,也是父類的實例

缺點(1) 無法實現多繼承 (2) 創建子類實例時,無法向父類構造函數傳參

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

實現

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

優點(1) 結合了構造+原型鏈繼承的優點

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

寄生組合繼承

實現

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;關鍵代碼,上面寫Super.prototype 也會變(引用類型,指向同一地址)

優點(1) 這應該是實現繼承最完美的方案了,es6的extends關鍵字,在babel轉換后代碼也是通過這種方式實現的繼承。

額外:借助(Object.create)

實現

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:{ // 構造函數修復 value: Child }})var child = new Child('min',23);console.log(child instanceof Super); // trueconsole.log(child instanceof Child); // trueconsole.log(child.constructor); // Child

以上就是JavaScript 實現繼承的幾種方式的詳細內容,更多關于JavaScript 實現繼承的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 成人午夜毛片 | 一级做a爰片性色毛片黄书 一级做a爰片性色毛片新版的 | 国产精品亚洲欧美日韩一区在线 | 午夜国产大片免费观看 | 国产免费又色又爽又黄在线观看 | 欧美日韩亚洲另类人人澡 | 香蕉大黄香蕉在线观看 | 久久草精品 | 青青青青青国产免费手机看视频 | 激情综合色五月丁香六月亚洲 | 国产高清一区二区三区四区 | 久久婷婷色一区二区三区 | 久久精品91 | 自拍视频网 | 成年免费大片黄在线观看岛国 | 日韩欧美一区二区三区在线视频 | 日本一级大黄毛片免费基地 | 日韩欧美一区黑人vs日本人 | 免费黄色在线网址 | 国产1024一区二区你懂的 | xxxx国产片 | 免费又黄又硬又大爽日本 | 8888四色奇米在线观看免费看 | 一区二区三区免费视频网站 | 久久精品国产欧美成人 | 中文字幕日韩欧美 | 尹人香蕉久久99天天拍欧美p7 | 免费一级毛片清高播放 | 久久er精品热线免费 | 日本免费va毛片在线看大 | 国产精品七七在线播放 | 亚洲乱理伦片在线看中字 | 欧美日韩精品一区二区 | 国产一区2区 | 黄色网址在线免费观看 | 亚洲国产婷婷俺也色综合 | 色综合久久久久久久久五月性色 | 久久久久久一级毛片免费野外 | 成人午夜大片免费看爽爽爽 | 国产精品不卡高清在线观看 | 欧美黄色大片免费看 |