国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

JavaScript 實現繼承的幾種方式

瀏覽:58日期: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
相關文章:
主站蜘蛛池模板: 日本xxxxx黄区免费看动漫 | 美女视频网站永久免费观看软件 | 国产精品视频999 | 牛人国产偷窥女洗浴在线观看 | 日韩手机看片福利精品 | 国产成年网站v片在线观看 国产成人aa在线视频 | 高清一本之道加勒比在线 | 日本免费一区视频 | 狠狠狠狠狠 | 99这里只有精品66视频 | 欧美精品v欧洲精品 | 成人性动漫高清免费观看网址 | 中文字幕 日韩在线 | 国产三级在线观看 | 免费观看一级一片 | 爽爽视频在线观看 | 国产免费一区二区三区在线观看 | 精品一区二区三区免费视频 | 久久草在线看 | 一区二区三区免费观看 | 国产看午夜精品理论片 | 黄色一级毛片免费 | 自拍偷自拍亚洲精品10p | 成人欧美在线 | 亚洲天堂色网站 | 欧美日韩午夜视频 | 国产欧美日韩在线观看精品 | 香港三级日本三级三级人妇 | 国产精品欧美一区二区三区不卡 | 亚洲国产精久久久久久久 | 亚州一级毛片在线 | 国产亚洲一区二区手机在线观看 | 成人欧美视频免费看黄黄 | caoporen在线视频入口 | 亚洲成年人专区 | 三级黄色片在线免费观看 | 国产在线日韩 | 午夜一级做a爰片久久毛片 午夜伊人网 | 欧美精品久久天天躁 | 国产欧美日韩亚洲精品区2345 | 欧美黄网在线 |