面向對象編程很重要的一個方面,就是對象的繼承。A 對象經過繼承 B 對象,就能直接擁有 B 對象的全部屬性和方法。這對於代碼的複用是很是有用的。javascript
大部分面向對象的編程語言,都是經過「類」(class)實現對象的繼承。傳統上,JavaScript 語言的繼承不經過 class(ES6 引入了class 語法),而是經過「原型對象」(prototype)實現。那麼在JS中常見的繼承方式有幾種呢?html
如需本文源碼,請猛戳 常見的六種繼承方式java
若是以爲文章對你有些許幫助,歡迎在個人GitHub博客點贊和關注,感激涕零!git
這種方式關鍵在於:子類型的原型爲父類型的一個實例對象。es6
//父類型
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() // 子類型的原型爲父類型的一個實例對象
var s1 = new Student(15000)
var s2 = new Student(14000)
console.log(s1,s2)
複製代碼
子類繼承父類的屬性和方法是將父類的私有屬性和公有方法都做爲本身的公有屬性和方法,咱們都知道在操做基本數據類型的時候操做的是值,在操做引用數據類型的時候操做的是地址,若是說父類的私有屬性中有引用類型的屬性,那它被子類繼承的時候會做爲公有屬性,這樣子類1操做這個屬性的時候,就會影響到子類2。github
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
複製代碼
另外注意一點的是,咱們須要在子類中添加新的方法或者是重寫父類的方法時候,切記必定要放到替換原型的語句以後編程
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 () { }//在這裏寫子類的原型方法和屬性是無效的,
//由於會改變原型的指向,因此應該放到從新指定以後
Student.prototype = new Person()
Student.prototype.sayHello = function () { }
var s1 = new Student(15000)
console.log(s1)
複製代碼
特色:瀏覽器
缺點:bash
Student.prototype = new Person()
以後執行,不能放到構造器中這種方式關鍵在於:在子類型構造函數中通用call()調用父類型構造函數app
<script type="text/javascript">
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) // 至關於: this.Person(name, age)
/*this.name = name
this.age = age*/
this.price = price
}
var s1 = new Student('Tom', 20, 15000)
複製代碼
console.log(s1.setAge())//Uncaught TypeError: s1.setAge is not a function
複製代碼
特色:
缺點:
這種方式關鍵在於:經過調用父類構造,繼承父類的屬性並保留傳參的優勢,而後經過將父類實例做爲子類原型,實現函數複用。
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//組合繼承也是須要修復構造函數指向的
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
複製代碼
優勢:
缺點:
這種方式經過父類原型和子類原型指向同一對象,子類能夠繼承到父類的公有方法當作本身的公有方法,並且不會初始化兩次實例方法/屬性,避免的組合繼承的缺點。
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)
複製代碼
console.log(s1 instanceof Student, s1 instanceof Person)//true true
console.log(s1.constructor)//Person
複製代碼
優勢:
缺點:
藉助原型能夠基於已有的對象來建立對象,var B = Object.create(A)
以A對象爲原型,生成了B對象。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原型對象的屬性和方法。目前來講,最完美的繼承方法!
ES6中引入了class關鍵字,class能夠經過extends關鍵字實現繼承,還能夠經過static關鍵字定義類的靜態方法,這比 ES5 的經過修改原型鏈實現繼承,要清晰和方便不少。
ES5 的繼承,實質是先創造子類的實例對象this,而後再將父類的方法添加到this上面(Parent.apply(this))。ES6 的繼承機制徹底不一樣,實質是先將父類實例對象的屬性和方法,加到this上面(因此必須先調用super方法),而後再用子類的構造函數修改this。
須要注意的是,class關鍵字只是原型的語法糖,JavaScript繼承仍然是基於原型實現的。
class Person {
//調用類的構造方法
constructor(name, age) {
this.name = name
this.age = age
}
//定義通常的方法
showName() {
console.log("調用父類的方法")
console.log(this.name, this.age);
}
}
let p1 = new Person('kobe', 39)
console.log(p1)
//定義一個子類
class Student extends Person {
constructor(name, age, salary) {
super(name, age)//經過super調用父類的構造方法
this.salary = salary
}
showName() {//在子類自身定義方法
console.log("調用子類的方法")
console.log(this.name, this.age, this.salary);
}
}
let s1 = new Student('wade', 38, 1000000000)
console.log(s1)
s1.showName()
複製代碼
缺點: