JavaScript中常見的幾種繼承方式
原型繼承
function Parent(name) { this.name = name } Parent.prototype.getName = function(){ return 'parentPrototype' + ' ' + this.name; } function Son() { } Son.prototype = new Parent(); console.log(new Son())
內(nèi)存圖
分析
原型繼承的原理就是把子類的prototype指向父類的實例身上,這樣做,子類就可以使用父類的屬性和方法了,但是,無法初始化父類的屬性,這個時候,盜用構(gòu)造函數(shù)繼承就出現(xiàn)了
盜用構(gòu)造函數(shù)繼承
function Parent(name) { this.name = name } Parent.prototype.getName = function(){ return 'parentPrototype' + ' ' + this.name; } function Son(name) { Parent.call(this, name) } console.log(new Son('jack'))
分析
盜用構(gòu)造函數(shù)的原理簡單理解就是吧父類函數(shù)的代碼 copy
到子類里面,在上述代碼中相當(dāng)于把父類的this.name = name
在子類里執(zhí)行了一次,這樣的話,就可以在子類實例化的時候初始化父類的一些屬性
如果這樣的話就取不到父類原型身上的方法了,為了解決這個問題,組合繼承就出現(xiàn)了
組合繼承
function Parent(name) { this.name = name } Parent.prototype.getName = function(){ return 'parentPrototype' + ' ' + this.name; } function Son(name) { Parent.call(this, name) // 第二次調(diào)用 } Son.prototype = new Parent() // 第一次調(diào)用 console.log(new Son('jack'))
組合繼承,也稱為偽經(jīng)典繼承,結(jié)合了原型繼承和盜用構(gòu)造函數(shù)繼承,既可以初始化父類的方法,又可以取到父類的原型身上的方法
可以看到這種繼承方式name會被多次創(chuàng)建,雖然在使用上沒什么區(qū)別,但是會多開辟一些無用內(nèi)存,而且還會多調(diào)用一次函數(shù)
原型鏈繼承
const parent = { name: 'jack', getName() { return this.name } } const son = Object.create(parent) console.log(son)
這種繼承適用于想在一個對象的基礎(chǔ)上添加一個新的對象,這個新的對象可以有自己獨立的屬性
還可以有自己的屬性:
const parent = { name: 'jack', getName() { return this.name } } const son = Object.create(parent, { age: { value: 18, enumerable: true, writable: true, configurable: true } }) console.log(son)
寄生式繼承
const parent = { name: 'jack', getName() { return this.name } } function createObj(origin){ const res = Object(origin); res.age = 18; res.getAge = function() { return this.age }; return res } const son = createObj(parent) console.log(son)
寄生式繼承就是類似于工廠模式,經(jīng)過它創(chuàng)建的函數(shù),會有一些自帶的值
寄生組合式繼承
function Parent(name) { this.name = name; } Parent.prototype.getName = function () { return this.name; } function Son(name) { Parent.call(this, name) }; function inhertPrototype(Son, Parent) { const res = Object(Parent.prototype); // 創(chuàng)建目標(biāo)對象的原型對象 // 可以理解為復(fù)制父類對象prototype的屬性,這樣做可以少調(diào)用一次父類的實例化操作 res.constructor = Son // 將構(gòu)造函數(shù)指向son Son.prototype = res //將子類的prototype 指向父類創(chuàng)建的原型對象,這樣子類既可以使用父類的方法,又不用實例化父類 } inhertPrototype(Son, Parent) console.log(new Son('jack'))
寄生組合式繼承 被稱為最佳繼承模式 他解決了組合繼承父類函數(shù)被調(diào)用兩次的問題,同時又不會給子類的prototype
加上父類的無用的屬性
可以對比組合繼承的圖看一下,prototype
上少了 name
,最佳的繼承方式
到此這篇關(guān)于JavaScript中常見的幾種繼承方式的文章就介紹到這了,更多相關(guān)js繼承方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于document.cookie的使用javascript
構(gòu)造通用的cookie處理函數(shù) cookie的處理過程比較復(fù)雜,并具有一定的相似性。因此可以定義幾個函數(shù)來完成cookie的通用操作,從而實現(xiàn)代碼的復(fù)用。2010-10-10微信小程序中使用echarts方法(全網(wǎng)最詳細(xì)教程!)
現(xiàn)在越來越多的項目都在使用可視化的功能,那么使用echarts實現(xiàn)是一種不錯的選擇,下面這篇文章主要給大家介紹了關(guān)于小程序中使用echarts的方法,本文介紹的方法應(yīng)該是全網(wǎng)最詳細(xì)教程,需要的朋友可以參考下2023-06-06在 javascript 中如何快速獲取數(shù)組指定位置的元素
這篇文章主要介紹了在 javascript 中快速獲取數(shù)組指定位置的元素,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04