Javascript的ES5,ES6的7種繼承詳解
眾所周知,在ES6之前,前端是不存在類的語法糖,所以不能像其他語言一樣用extends關(guān)鍵字就搞定繼承關(guān)系,需要一些額外的方法來實(shí)現(xiàn)繼承。下面就介紹一些常用的方法,紅寶書已經(jīng)概括的十分全面了,所以本文基本就是對(duì)紅寶書繼承篇章的筆記和梳理。
原型鏈繼承
function Parent() {
this.name = 'arzh'
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child() {
}
//主要精髓所在
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild = new Child()
arzhChild.getName() // 'arzh'
原型鏈繼承缺點(diǎn):
1.每個(gè)實(shí)例對(duì)引用類型屬性的修改都會(huì)被其他的實(shí)例共享
function Parent() {
this.names = ['arzh','arzh1'];
}
function Child() {
}
//主要精髓所在
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild2 = new Child()
arzhChild2.names.push('arzh2')
console.log(arzhChild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhChild3 = new Child()
arzhChild3.names.push('arzh3')
console.log(arzhChild3.names) //[ 'arzh', 'arzh1', 'arzh2', 'arzh3' ]
2.在創(chuàng)建Child實(shí)例的時(shí)候,無法向Parent傳參。這樣就會(huì)使Child實(shí)例沒法自定義自己的屬性(名字)
借用構(gòu)造函數(shù)(經(jīng)典繼承)
function Parent() {
this.names = ['arzh','arzh1']
}
function Child() {
Parent.call(this)
}
var arzhChild2 = new Child()
arzhChild2.names.push('arzh2')
console.log(arzhChild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhChild3 = new Child()
arzhChild3.names.push('arzh3')
console.log(arzhChild3.names) //[ 'arzh', 'arzh1', 'arzh3' ]
優(yōu)點(diǎn):
- 解決了每個(gè)實(shí)例對(duì)引用類型屬性的修改都會(huì)被其他的實(shí)例共享的問題
- 子類可以向父類傳參
function Parent(name) {
this.name = name
}
function Child(name) {
Parent.call(this, name)
}
var arzhChild = new Child('arzh');
console.log(arzhChild.name); // arzh
var arzhChild1 = new Child('arzh1');
console.log(arzhChild1.name); // arzh1
缺點(diǎn):
- 無法復(fù)用父類的公共函數(shù)
- 每次子類構(gòu)造實(shí)例都得執(zhí)行一次父類函數(shù)
組合式繼承(原型鏈繼承和借用構(gòu)造函數(shù)合并)
function Parent(name) {
this.name = name
this.body = ['foot','hand']
}
function Child(name, age) {
Parent.call(this, name)
this.age = age
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
var arzhChild1 = new Child('arzh1', '18')
arzhChild1.body.push('head1')
console.log(arzhChild1.name,arzhChild1.age) //arzh1 18
console.log(arzhChild1.body) //[ 'foot', 'hand', 'head1' ]
var arzhChild2 = new Child('arzh2', '20')
arzhChild2.body.push('head2')
console.log(arzhChild2.name,arzhChild2.age) //arzh2 20
console.log(arzhChild2.body) //[ 'foot', 'hand', 'head2' ]
優(yōu)點(diǎn):
- 解決了每個(gè)實(shí)例對(duì)引用類型屬性的修改都會(huì)被其他的實(shí)例共享的問題
- 子類可以向父類傳參
- 可實(shí)現(xiàn)父類方法復(fù)用
缺點(diǎn):
- 需執(zhí)行兩次父類構(gòu)造函數(shù),第一次是
Child.prototype = new Parent(),第二次是Parent.call(this, name)造成不必要的浪費(fèi)
原型式繼承
復(fù)制傳入的對(duì)象到創(chuàng)建對(duì)象的原型上,從而實(shí)現(xiàn)繼承
function createObj(o) {
function F(){}
F.prototype = o;
return new F();
}
var person = {
name : 'arzh',
body : ['foot','hand']
}
var person1 = createObj(person)
var person2 = createObj(person)
console.log(person1) //arzh
person1.body.push('head')
console.log(person2) //[ 'foot', 'hand', 'head' ]
缺點(diǎn): 同原型鏈繼承一樣,每個(gè)實(shí)例對(duì)引用類型屬性的修改都會(huì)被其他的實(shí)例共享
寄生式繼承
我們可以使用Object.create來代替上述createObj的實(shí)現(xiàn),原理基本上是一樣的。寄生式繼承其實(shí)就是在createObj的內(nèi)部以某種形式來增強(qiáng)對(duì)象(這里的增強(qiáng)可以理解為添加對(duì)象的方法),最后返回增強(qiáng)之后的對(duì)象。
function createEnhanceObj(o) {
//代替原型式繼承的createObj
var clone = Object.create(o)
clone.getName = function () {
console.log('arzh')
}
return clone;
}
通過createEnhanceObj就可以在創(chuàng)建對(duì)象的時(shí)候,把對(duì)象方法也通過此種方式繼承。
缺點(diǎn): 同借用構(gòu)造函數(shù)一樣,無法復(fù)用父類函數(shù),每次創(chuàng)建對(duì)象都會(huì)創(chuàng)建一遍方法
寄生組合式繼承
不需要為了子類的原型而多new了一次父類的構(gòu)造函數(shù),如Child.prototype = new Parent() 只需要復(fù)制父類原型的一個(gè)副本給子類原型即可
function inheritPrototype(Parent, Child){
Child.prototype = Object.create(Parent.prototype) //創(chuàng)建父類原型的一個(gè)副本,把副本賦值給子類原型
Child.prototype.constructor = Child;
}
function Parent(name) {
this.name = name
}
Parent.prototype.getName = function () {
console.log(this.name)
}
function Child(color) {
Parent.call(this, 'arzh')
this.color = color
}
inheritPrototype(Parent, Child)
var arzhChild = new Child('red')
console.log(arzhChild.name) // 'arzh'
優(yōu)點(diǎn): 不必為了指定子類型的原型而調(diào)用父類型的構(gòu)造函數(shù)
ES6繼承
ES6支持通過類來實(shí)現(xiàn)繼承,方法比較簡(jiǎn)單,代碼如下
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
toString() {
return this.x + '' + this.y
}
}
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y) //調(diào)用父類的constructor(x, y)
this.color = color
}
toString() {
return this.color + ' ' + super.toString() // 調(diào)用父類的toString()
}
}
var colorPoint = new ColorPoint('1', '2', 'red')
console.log(colorPoint.toString()) // red 12
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- JavaScript實(shí)現(xiàn)簡(jiǎn)易輪播圖最全代碼解析(ES5)
- 詳解JavaScript之ES5的繼承
- js定義類的方法示例【ES5與ES6】
- 詳解vue-cli+es6引入es5寫的js(兩種方法)
- JS實(shí)現(xiàn)集合的交集、補(bǔ)集、差集、去重運(yùn)算示例【ES5與ES6寫法】
- JavaScript設(shè)計(jì)模式之原型模式分析【ES5與ES6】
- js es6系列教程 - 基于new.target屬性與es5改造es6的類語法
- JavaScript ES5標(biāo)準(zhǔn)中新增的Array方法
- JS?ES5創(chuàng)建常量詳解
相關(guān)文章
javascript操作html控件實(shí)例(javascript添加html)
幾乎HTML所有標(biāo)記都可以說是HTML的控件,如select, input, div, table等。html標(biāo)簽便捷的操作,深受大家的喜歡。如何使用javascript來操作HTML控件,下面我介紹下比較麻煩的幾個(gè)控件2013-12-12
javascript之函數(shù)直接量(function(){})()
javascript之函數(shù)直接量(function(){})()...2007-06-06
javascript學(xué)習(xí)筆記(二) js一些基本概念
javascript學(xué)習(xí)筆記之js一些基本概念,學(xué)習(xí)js的朋友可以參考下2012-06-06

