分分鐘學(xué)會繼承

這是我見到的幾種js繼承方法,如果有文章中沒有提及的,希望可以分享共同學(xué)習(xí)

(一)原型鏈繼承

function Parent(name) {
this.name = name;
}
Parent.prototype.getName = function() {
console.log("parent name is " + this.name)
}
function Child(name) {
this.name = name
}
Child.prototype = new Parent('張三')
Child.prototype.constructor = Child
Child.prototype.getName = function() {
console.log('child name is ' + this.name)
}
var child = new Child('小張')
child.getName() // child name is 小張

只要是原型鏈中出現(xiàn)過的原型,都可以說是該原型鏈派生的實例的原型。
這種方法有個缺點(diǎn):

  1. 子類型無法給超類型傳遞參數(shù),在面向?qū)ο蟮睦^承中,我們總希望通過 var child = new Child('son', 'father'); 讓子類去調(diào)用父類的構(gòu)造器來完成繼承。

(二) 類是繼承

function Parent(name) {
this.name = name;
}
Parent.prototype.getName = function() {
console.log("parent name is " + this.name)
}
Parent.prototype.do = function() {
console.log("doSomething")
}
function Child(name, parentName) {
Parent.call(this, parentName)
this.name = name
}
Child.prototype.getName = function() {
console.log('child name is ' + this.name)
}
var child = new Child('小張')
child.getName() // child name is 小張
child.do() // child.do is not a functio

相當(dāng)于 Parent 這個函數(shù)在 Child 函數(shù)中執(zhí)行了一遍,并且將所有與 this 綁定的變量都切換到了 Child 上,這樣就克服了第一種方式帶來的問題。
缺點(diǎn):
1. Parent函數(shù)在Child中執(zhí)行一遍, 并且不能復(fù)用一些共有的函數(shù)

(三)組合式繼承, 一二兩種方法的結(jié)合

function Parent(name) {
this.name = name;
}
Parent.prototype.getName = function() {
console.log("parent name is " + this.name)
}
Parent.prototype.do = function() {
console.log("doSomething")
}
function Child(name, parentName) {
Parent.call(this, parentName)
this.name = name
}
Child.prototype.getName = function() {
console.log('child name is ' + this.name)
}
Child.prototype = new Parent();
Child.prototype.construtor = Child;
var child = new Child('小張')
child.getName() // child name is 小張
child.do() // doSomething

缺點(diǎn):
Parent 需要調(diào)用兩次

(四)寄生組合式繼承

function Parent(name) {
this.name = name;
}
Parent.prototype.getName = function() {
console.log("parent name is " + this.name)
}
Parent.prototype.do = function() {
console.log("doSomething")
}
function Child(name, parentName) {
Parent.call(this, parentName)
this.name = name
}
function initPrototype(Parent, Child) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.construtor = Child;
}
initPrototype(Parent, Child);
Child.prototype.getName = function() {
console.log('child name is ' + this.name)
}
var child = new Child('小張', '張三')
child.getName() // child name is 小張
child.do() // doSomething

(五)ES6繼承

class Parent {
constructor(name) {
this.name = name;
}
do() {
console.log('something');
}
getName() {
console.log('parent name is', this.name);
}
}
class Child extends Parent {
constructor(name, parentName) {
super(parentName);
this.name = name;
}
getName() {
console.log('child name is', this.name);
}
}
const child = new Child('小張', '張三');
child.getName(); // child name is 小張
child.do(); // something

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • (a fork of Airbnb's Javascript Style Guide) Strikingly ES...
    飄零_zyw閱讀 1,271評論 1 2
  • 工廠模式類似于現(xiàn)實生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情,實現(xiàn)同樣的效果;這時候需要使用工廠模式。簡單...
    舟漁行舟閱讀 8,116評論 2 17
  • 單例模式 適用場景:可能會在場景中使用到對象,但只有一個實例,加載時并不主動創(chuàng)建,需要時才創(chuàng)建 最常見的單例模式,...
    Obeing閱讀 2,313評論 1 10
  • 我基本從來不寫工作的事兒。 因為工作實在沒啥好寫的,不就是工作唄。 然後今天打算稍微寫一點(diǎn),就寫JS吧。 我一直相...
    LostAbaddon閱讀 1,551評論 22 21
  • 一個夢。我總愛記錄自己各種奇怪的,但是卻有印象的夢。 也許是因為睡覺也戴著耳機(jī),所以,常常會因為歌入夢而夢到故事。...
    _akc_閱讀 201評論 0 2

友情鏈接更多精彩內(nèi)容