JavaScript六種常見(jiàn)的繼承方法分享
繼承是面向?qū)ο缶幊讨械囊粋€(gè)重要概念,它允許一個(gè)對(duì)象(子類或派生類)獲取另一個(gè)對(duì)象(父類或基類)的屬性和方法。在 JavaScript 中,有多種方式可以實(shí)現(xiàn)繼承。以下是六種常見(jiàn)的 JavaScript 繼承方法以及詳細(xì)介紹:
原型鏈繼承(Prototype Inheritance):
這是 JavaScript 中最基本的繼承方式。它通過(guò)將子類的原型對(duì)象指向父類的實(shí)例來(lái)實(shí)現(xiàn)繼承。這意味著子類繼承了父類的屬性和方法,但也可能會(huì)有一些潛在的問(wèn)題,比如所有子類的實(shí)例共享父類屬性,導(dǎo)致意外的行為。
function Parent() {
this.name = 'Parent'
}
Parent.prototype.sayHello = function () {
console.log(`Hello, I'm ${this.name}`)
}
function Child() {
this.name = 'Child'
}
Child.prototype = new Parent()
const child = new Child()
child.sayHello() // 輸出 "Hello, I'm Child"構(gòu)造函數(shù)繼承(Constructor Inheritance):
這種繼承方式通過(guò)在子類構(gòu)造函數(shù)中調(diào)用父類構(gòu)造函數(shù)來(lái)實(shí)現(xiàn)。這樣可以避免原型鏈繼承中的共享屬性問(wèn)題,但父類的方法不會(huì)被繼承到子類的原型中。
function Parent() {
this.name = 'Parent';
}
function Child() {
Parent.call(this);
this.name = 'Child';
}
const child = new Child();
console.log(child.name); // 輸出 "Child"組合繼承(Combination Inheritance):
這是將原型鏈繼承和構(gòu)造函數(shù)繼承結(jié)合的一種方式。它使用原型鏈繼承來(lái)繼承方法,使用構(gòu)造函數(shù)繼承來(lái)繼承屬性。但這種方法會(huì)調(diào)用兩次父類構(gòu)造函數(shù),可能會(huì)導(dǎo)致性能問(wèn)題。
function Parent() {
this.name = 'Parent';
}
Parent.prototype.sayHello = function() {
console.log(`Hello, I'm ${this.name}`);
}
function Child() {
Parent.call(this);
this.name = 'Child';
}
Child.prototype = new Parent();
const child = new Child();
child.sayHello(); // 輸出 "Hello, I'm Child"原型式繼承(Prototype-based Inheritance):
這種繼承方式使用一個(gè)已有的對(duì)象作為模板來(lái)創(chuàng)建新對(duì)象,新對(duì)象可以繼承模板對(duì)象的屬性和方法。但需要注意的是,修改子對(duì)象的屬性也會(huì)影響到模板對(duì)象。
const parent = {
name: 'Parent',
sayHello: function() {
console.log(`Hello, I'm ${this.name}`);
}
};
const child = Object.create(parent);
child.name = 'Child';
child.sayHello(); // 輸出 "Hello, I'm Child"
```js寄生式繼承(Parasitic Inheritance):
這種方式類似于原型式繼承,但在創(chuàng)建新對(duì)象時(shí)可以添加額外的屬性和方法,這些額外的屬性和方法可以根據(jù)需要進(jìn)行定制。
function createChild(parent) {
const child = Object.create(parent);
child.name = 'Child';
child.sayHello = function() {
console.log(`Hello, I'm ${this.name}`);
};
return child;
}
const parent = {
name: 'Parent'
};
const child = createChild(parent);
child.sayHello(); // 輸出 "Hello, I'm Child"ES6 類繼承(Class Inheritance):
ES6 引入了 class 關(guān)鍵字,使面向?qū)ο缶幊谈雍?jiǎn)潔??梢允褂?extends 關(guān)鍵字實(shí)現(xiàn)類的繼承,子類可以繼承父類的屬性和方法。
class Parent {
constructor() {
this.name = 'Parent';
}
sayHello() {
console.log(`Hello, I'm ${this.name}`);
}
}
class Child extends Parent {
constructor() {
super();
this.name = 'Child';
}
}
const child = new Child();
child.sayHello(); // 輸出 "Hello, I'm Child"學(xué)習(xí)這些繼承模式的關(guān)鍵在于理解它們的核心概念,否則當(dāng)你在編寫代碼時(shí)遇到書本上的示例時(shí),可能會(huì)感到為什么不直接采用更簡(jiǎn)單的繼承方式呢?以原型式繼承為例,它通過(guò)復(fù)制內(nèi)部對(duì)象的一個(gè)副本來(lái)實(shí)現(xiàn)繼承。這種方式不僅可以繼承內(nèi)部對(duì)象的屬性,還能夠自由調(diào)用其中包含的函數(shù)、對(duì)象,以及從源內(nèi)部對(duì)象返回的內(nèi)容。你可以添加屬性,修改參數(shù),從而定制化原型對(duì)象,而這些新增的屬性不會(huì)相互影響。
以上就是JavaScript六種常見(jiàn)的繼承方法分享的詳細(xì)內(nèi)容,更多關(guān)于JavaScript繼承方法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
CocosCreator ScrollView優(yōu)化系列之分幀加載
這篇文章主要介紹了CocosCreator ScrollView的優(yōu)化,從分幀加載進(jìn)行了講解,對(duì)性能優(yōu)化感興趣的同學(xué),一定要看一下2021-04-04
js實(shí)現(xiàn)iGoogleDivDrag模塊拖動(dòng)層拖動(dòng)特效的方法
這篇文章主要介紹了js實(shí)現(xiàn)iGoogleDivDrag模塊拖動(dòng)層拖動(dòng)特效的方法,實(shí)例分析了javascript操作拖動(dòng)層的技巧,需要的朋友可以參考下2015-03-03
javascript中Array數(shù)組的迭代方法實(shí)例分析
這篇文章主要介紹了javascript中Array數(shù)組的迭代方法,實(shí)例分析了Array數(shù)組的迭代方法定義與使用技巧,需要的朋友可以參考下2015-02-02
JavaScript數(shù)組push方法使用注意事項(xiàng)
push() 方法可向數(shù)組的末尾添加一個(gè)或多個(gè)元素,并返回新的長(zhǎng)度。這篇文章主要介紹了JavaScript數(shù)組push方法使用注意,需要的朋友可以參考下2017-10-10
JS實(shí)現(xiàn)彈出浮動(dòng)窗口(支持鼠標(biāo)拖動(dòng)和關(guān)閉)實(shí)例詳解
這篇文章主要介紹了JS實(shí)現(xiàn)彈出浮動(dòng)窗口,可支持鼠標(biāo)拖動(dòng)和關(guān)閉的功能,界面美觀大方,涉及javascript動(dòng)態(tài)創(chuàng)建對(duì)話框的相關(guān)技巧,需要的朋友可以參考下2015-08-08
JavaScript生成器函數(shù)Generator解決異步操作問(wèn)題
這篇文章主要為大家介紹了JavaScript生成器函數(shù)Generator解決異步操作問(wèn)題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
BootStrap學(xué)習(xí)系列之布局組件(下拉,按鈕組[toolbar],上拉)
這篇文章主要介紹了BootStrap學(xué)習(xí)系列之布局組件(下拉,按鈕組[toolbar],上拉)的相關(guān)資料,需要的朋友可以參考下2017-01-01

