JavaScript 實現(xiàn)繼承的幾種方式
非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); // false console.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); // true console.log(child instanceof Child); // true console.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); // true console.log(child instanceof Child); // true console.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;關鍵代碼,上面寫Super.prototype 也會變(引用類型,指向同一地址)
優(yōu)點
(1) 這應該是實現(xiàn)繼承最完美的方案了,es6的extends關鍵字,在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ù)修復 value: Child } }) var child = new Child("min",23); console.log(child instanceof Super); // true console.log(child instanceof Child); // true console.log(child.constructor); // Child
以上就是JavaScript 實現(xiàn)繼承的幾種方式的詳細內(nèi)容,更多關于JavaScript 實現(xiàn)繼承的資料請關注腳本之家其它相關文章!
相關文章
JS簡單限制textarea內(nèi)輸入字符數(shù)量的方法
這篇文章主要介紹了JS簡單限制textarea內(nèi)輸入字符數(shù)量的方法,涉及JavaScript響應鼠標及鍵盤事件處理textarea輸入框字符的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10JavaScript中this的用法及this在不同應用場景的作用解析
由于其運行期綁定的特性,JavaScript 中的 this 含義要豐富得多,它可以是全局對象、當前對象或者任意對象,這完全取決于函數(shù)的調(diào)用方式,這篇文章主要給大家介紹了JavaScript中this的用法及this在不同應用場景的作用解析,一起看看吧2017-04-04JS中style.display和style.visibility的區(qū)別實例說明
下面的例子說明了這種區(qū)別:在這個例子中,divContent1和divContent2隱藏的時候用的是style.display=none,這時候,后面的div會向上移動,占據(jù)已經(jīng)隱藏的div的空間。divContent3和divContent4用的是style.visibility=hidden來隱藏,但是其隱藏后仍然占據(jù)原來的空間2013-03-03