前言
面向?qū)ο缶幊毯苤匾囊粋€(gè)方面,就是對(duì)象的繼承灵莲。A 對(duì)象通過(guò)繼承 B 對(duì)象,就能直接擁有 B 對(duì)象的所有屬性和方法明场。這對(duì)于代碼的復(fù)用是非常有用的苦锨。
大部分面向?qū)ο蟮木幊陶Z(yǔ)言拉庶,都是通過(guò)“類”(class)實(shí)現(xiàn)對(duì)象的繼承砍的。傳統(tǒng)上,JavaScript 語(yǔ)言的繼承不通過(guò) class(ES6 引入了class 語(yǔ)法)床佳,而是通過(guò)“原型對(duì)象”(prototype)實(shí)現(xiàn)。那么在JS中常見(jiàn)的繼承方式有幾種呢搁进?
方式一、原型鏈繼承
這種方式關(guān)鍵在于:子類型的原型為父類型的一個(gè)實(shí)例對(duì)象莱革。
//父類型
function Person(name, age) {
this.name = name,
this.age = age,
this.play = [1, 2, 3]
this.setName = function () {}
}
Person.prototype.setAge = function () {}
//子類型
function Student(price) {
this.price = price
this.setScore = function () {}
}
Student.prototype = new Person() // 子類型的原型為父類型的一個(gè)實(shí)例對(duì)象
var s1 = new Student(15000)
var s2 = new Student(14000)
console.log(s1,s2)
但這種方式實(shí)現(xiàn)的本質(zhì)是通過(guò)將子類的原型指向了父類的實(shí)例,所以子類的實(shí)例就可以通過(guò)proto__訪問(wèn)到 Student.prototype 也就是Person的實(shí)例,這樣就可以訪問(wèn)到父類的私有方法赏半,然后再通過(guò)__proto指向父類的prototype就可以獲得到父類原型上的方法牧氮。于是做到了將父類的私有丹莲、公有方法和屬性都當(dāng)做子類的公有屬性
子類繼承父類的屬性和方法是將父類的私有屬性和公有方法都作為自己的公有屬性和方法甥材,我們都知道在操作基本數(shù)據(jù)類型的時(shí)候操作的是值,在操作引用數(shù)據(jù)類型的時(shí)候操作的是地址叠萍,如果說(shuō)父類的私有屬性中有引用類型的屬性,那它被子類繼承的時(shí)候會(huì)作為公有屬性腹殿,這樣子類1操作這個(gè)屬性的時(shí)候,就會(huì)影響到子類2自沧。
s1.play.push(4)
console.log(s1.play, s2.play)
console.log(s1.__proto__ === s2.__proto__)//true
console.log(s1.__proto__.__proto__ === s2.__proto__.__proto__)//true
s1中play屬性發(fā)生變化筏勒,與此同時(shí)管行,s2中play屬性也會(huì)跟著變化捐顷。
另外注意一點(diǎn)的是,我們需要在子類中添加新的方法或者是重寫父類的方法時(shí)候叮姑,切記一定要放到替換原型的語(yǔ)句之后
function Person (name, age) {
this.name = name,
this.age = age
}
Person.prototype.setAge = function () {
console.log("111")
}
function Student (price) {
this.price = price
this.setScore = function () { }
}
// Student.prototype.sayHello = function () { }//在這里寫子類的原型方法和屬性是無(wú)效的极颓,
//因?yàn)闀?huì)改變?cè)偷闹赶虿ぢ。詰?yīng)該放到重新指定之后
Student.prototype = new Person()
Student.prototype.sayHello = function () { }
var s1 = new Student(15000)
console.log(s1)
特點(diǎn):
父類新增原型方法/原型屬性骇径,子類都能訪問(wèn)到
簡(jiǎn)單,易于實(shí)現(xiàn)
缺點(diǎn):
無(wú)法實(shí)現(xiàn)多繼承
來(lái)自原型對(duì)象的所有屬性被所有實(shí)例共享
創(chuàng)建子類實(shí)例時(shí)运敢,無(wú)法向父類構(gòu)造函數(shù)傳參
要想為子類新增屬性和方法传惠,必須要在Student.prototype = new Person() 之后執(zhí)行,不能放到構(gòu)造器中
方式二: 借用構(gòu)造函數(shù)繼承
這種方式關(guān)鍵在于:在子類型構(gòu)造函數(shù)中通用call()調(diào)用父類型構(gòu)造函數(shù)
function Person(name, age) {
this.name = name,
this.age = age,
this.setName = function () {}
}
Person.prototype.setAge = function () {}
function Student(name, age, price) {
Person.call(this, name, age) // 相當(dāng)于: this.Person(name, age)
/*this.name = name
this.age = age*/
this.price = price
}
var s1 = new Student('Tom', 20, 15000)
這種方式只是實(shí)現(xiàn)部分的繼承,如果父類的原型還有方法和屬性浇坐,子類是拿不到這些方法和屬性的。
console.log(s1.setAge())//Uncaught TypeError: s1.setAge is not a function
特點(diǎn):
解決了原型鏈繼承中子類實(shí)例共享父類引用屬性的問(wèn)題
創(chuàng)建子類實(shí)例時(shí)觉渴,可以向父類傳遞參數(shù)
可以實(shí)現(xiàn)多繼承(call多個(gè)父類對(duì)象)
缺點(diǎn):
實(shí)例并不是父類的實(shí)例,只是子類的實(shí)例
只能繼承父類的實(shí)例屬性和方法踢京,不能繼承原型屬性和方法
無(wú)法實(shí)現(xiàn)函數(shù)復(fù)用漱挚,每個(gè)子類都有父類實(shí)例函數(shù)的副本,影響性能
方式三: 原型鏈+借用構(gòu)造函數(shù)的組合繼承
這種方式關(guān)鍵在于:通過(guò)調(diào)用父類構(gòu)造白华,繼承父類的屬性并保留傳參的優(yōu)點(diǎn),然后通過(guò)將父類實(shí)例作為子類原型虾攻,實(shí)現(xiàn)函數(shù)復(fù)用。
function Person (name, age) {
this.name = name,
this.age = age,
this.setAge = function () { }
}
Person.prototype.setAge = function () {
console.log("111")
}
function Student (name, age, price) {
Person.call(this, name, age)
this.price = price
this.setScore = function () { }
}
Student.prototype = new Person()
Student.prototype.constructor = Student//組合繼承也是需要修復(fù)構(gòu)造函數(shù)指向的
Student.prototype.sayHello = function () { }
var s1 = new Student('Tom', 20, 15000)
var s2 = new Student('Jack', 22, 14000)
console.log(s1)
console.log(s1.constructor) //Student
console.log(p1.constructor) //Person
這種方式融合原型鏈繼承和構(gòu)造函數(shù)的優(yōu)點(diǎn),是 JavaScript 中最常用的繼承模式顶别。不過(guò)也存在缺點(diǎn)就是無(wú)論在什么情況下驯绎,都會(huì)調(diào)用兩次構(gòu)造函數(shù):一次是在創(chuàng)建子類型原型的時(shí)候,另一次是在子類型構(gòu)造函數(shù)的內(nèi)部,子類型最終會(huì)包含父類型對(duì)象的全部實(shí)例屬性,但我們不得不在調(diào)用子類構(gòu)造函數(shù)時(shí)重寫這些屬性绽媒。
優(yōu)點(diǎn):
可以繼承實(shí)例屬性/方法是辕,也可以繼承原型屬性/方法
不存在引用屬性共享問(wèn)題
可傳參
函數(shù)可復(fù)用
缺點(diǎn):
調(diào)用了兩次父類構(gòu)造函數(shù)获三,生成了兩份實(shí)例
方式四: 組合繼承優(yōu)化1
這種方式通過(guò)父類原型和子類原型指向同一對(duì)象伞租,子類可以繼承到父類的公有方法當(dāng)做自己的公有方法葵诈,而且不會(huì)初始化兩次實(shí)例方法/屬性作喘,避免的組合繼承的缺點(diǎn)沪斟。
function Person (name, age) {
this.name = name,
this.age = age,
this.setAge = function () { }
}
Person.prototype.setAge = function () {
console.log("111")
}
function Student (name, age, price) {
Person.call(this, name, age)
this.price = price
this.setScore = function () { }
}
Student.prototype = Person.prototype
Student.prototype.sayHello = function () { }
var s1 = new Student('Tom', 20, 15000)
console.log(s1)
但這種方式?jīng)]辦法辨別是對(duì)象是子類還是父類實(shí)例化
console.log(s1 instanceof Student, s1 instanceof Person)//true true
console.log(s1.constructor)//Person
優(yōu)點(diǎn):
不會(huì)初始化兩次實(shí)例方法/屬性,避免的組合繼承的缺點(diǎn)
缺點(diǎn):
沒(méi)辦法辨別是實(shí)例是子類還是父類創(chuàng)造的,子類和父類的構(gòu)造函數(shù)指向是同一個(gè)粤攒。
方式五: 組合繼承優(yōu)化2
借助原型可以基于已有的對(duì)象來(lái)創(chuàng)建對(duì)象,var B = Object.create(A)以A對(duì)象為原型盔几,生成了B對(duì)象逊拍。B繼承了A的所有屬性和方法芯丧。
function Person (name, age) {
this.name = name,
this.age = age
}
Person.prototype.setAge = function () {
console.log("111")
}
function Student (name, age, price) {
Person.call(this, name, age)
this.price = price
this.setScore = function () { }
}
Student.prototype = Object.create(Person.prototype)//核心代碼
Student.prototype.constructor = Student//核心代碼
var s1 = new Student('Tom', 20, 15000)
console.log(s1 instanceof Student, s1 instanceof Person) // true true
console.log(s1.constructor) //Student
console.log(s1)
同樣的寿冕,Student繼承了所有的Person原型對(duì)象的屬性和方法。目前來(lái)說(shuō)玫恳,最完美的繼承方法!
方式六:ES6中class 的繼承
ES6中引入了class關(guān)鍵字惭婿,class可以通過(guò)extends關(guān)鍵字實(shí)現(xiàn)繼承财饥,還可以通過(guò)static關(guān)鍵字定義類的靜態(tài)方法,這比 ES5 的通過(guò)修改原型鏈實(shí)現(xiàn)繼承,要清晰和方便很多谦炒。
ES5 的繼承,實(shí)質(zhì)是先創(chuàng)造子類的實(shí)例對(duì)象this透且,然后再將父類的方法添加到this上面(Parent.apply(this))。ES6 的繼承機(jī)制完全不同锅论,實(shí)質(zhì)是先將父類實(shí)例對(duì)象的屬性和方法,加到this上面(所以必須先調(diào)用super方法)藻懒,然后再用子類的構(gòu)造函數(shù)修改this。
需要注意的是鄙早,class關(guān)鍵字只是原型的語(yǔ)法糖,JavaScript繼承仍然是基于原型實(shí)現(xiàn)的弥虐。
class Person {
//調(diào)用類的構(gòu)造方法
constructor(name, age) {
this.name = name
this.age = age
}
//定義一般的方法
showName () {
console.log("調(diào)用父類的方法")
console.log(this.name, this.age);
}
}
let p1 = new Person('kobe', 39)
console.log(p1)
//定義一個(gè)子類
class Student extends Person {
constructor(name, age, salary) {
super(name, age)//通過(guò)super調(diào)用父類的構(gòu)造方法
this.salary = salary
}
showName () {//在子類自身定義方法
console.log("調(diào)用子類的方法")
console.log(this.name, this.age, this.salary);
}
}
let s1 = new Student('wade', 38, 1000000000)
console.log(s1)
s1.showName()
優(yōu)點(diǎn):
語(yǔ)法簡(jiǎn)單易懂,操作更方便
缺點(diǎn):
并不是所有的瀏覽器都支持class關(guān)鍵字