JavaScript 繼承

繼承是JS中非常內容,原因就是JS沒有地道的繼承方式,我們只能通過各種方式來模擬面向對象中的繼承。下面介紹幾種常見的繼承方式及其不足。

構造函數(shù)繼承

function Parent1 (){
    this.name = 'parent'
}

function Child1() {
    Parent1.call(this);
    this.type = 'child1';
}

缺點:Parent1 原型鏈上的東西并不會繼承,這種方式,所以只實現(xiàn)了部分繼承,如果父類的屬性都在構造函數(shù)中,沒問題,但是如果有一部分在原型鏈上,那么就繼承不了,為了解決這個不足我們就要使用原型鏈來進行原型繼承。

原型繼承

function Parent2() {
this.name = 'Parent2';
this.res = [1, 2, 3]
}

function Child2 () {
this.type = 'Child2'
}

Child2.prototype = new Parent2()

var child2 = new Child2();
var child3 = new Child3();

child2.res.push(4);
console.log(child3.res) // 1,2,3,4

缺點 : 這種方式缺點也很明顯,實例的兩個對象,如果一個對象改變res的值,那么另一個對象 的res屬性也會被改變(這兩個對象共享一個原型),這違背了獨立性。

組合

function Parent3() {
    this.name = 'parent3';
    this.res = [1, 2, 3];
}

function Child3() {
    Parent3.call(this);
    this.type = 'child3';
}

Child3.prototype = Parent3.prototype;
var child = new Child3();

缺點:此時child.constructor并不是Child3;而是Parent3,這是因為當我們想獲取child.constructor實際上是訪問Child3.prototype.constructor(也就是說constructor這個屬性是存在于原型上,并不是直接在child這個對象上),而Child3.prototype此時等于Parent3.prototype,所以最后constructor的屬性值為Parent3。

組合優(yōu)化

function Parent4() {
this.name = 'parent4';
this.res = [1, 2, 3];
}

function Child4() {
 Parent4.call(this);
 this.type = 'child4';
}

Child4.prototype = Object.create(Parent4.prototype);
// Child4.prototype = Parent4.prototype;
Child4.prototype.constructor = Child4;  

在這里我們加上這句Child4.prototype = Object.create(Parent4.prototype);的目的是為了隔離子類原型和父類原型,現(xiàn)在就是Child4.prototype.__proto__ === Parent4.prototype,如果我們不加,直接修正子類的構造函數(shù)(Child4.prototype.constructor = Child4;)那么也會把父類的Parent4.prototype.constructor更改成Child4,因為此時Child4.prototypeParent4.prototype指向同一地址。

注: 如果這里不支持Object.create,我們可以采用下面的plolly
function F(){} F.prototype = Parent4.prototype Child4.prototype = new F()

缺點:貌似還沒有實現(xiàn)靜態(tài)屬性的繼承

實現(xiàn)靜態(tài)屬性的繼承

function Parent() {
  this.age = 20
}

Parent.sex = 'male';
Parent.habby = 'badminton';

function Child() {
  Parent.call(this);
  this.type = 'Child';

 if (Object.setPrototypeOf) {
   Object.setPrototypeOf(Child, Parent)
 } else if (Child.__proto__) {
   Child.__proto__ = Parent
 } else {
   for (var attr in Parent) {
     if (Parent.hasOwnProperty(attr) && !(attr in Child)) {
       Child[attr] = Parent[attr]
     }
   }
 }   
}

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var child = new Child();
var parent = new Parent();
console.log(child)
for (var key in Child) {
  console.log(key)
}

console.log(child.constructor)
console.log(parent.constructor)
console.log(child instanceof Child)
console.log(child instanceof Parent)

看似完美了,但是還有一個問題,就是如果后續(xù)父類繼續(xù)添加一些靜態(tài)的方法,是不會自動同步到子的靜態(tài)方法上面去的。

最后(歡迎大家關注我)

DJL簫氏個人博客
博客GitHub地址
簡書
掘金

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

相關閱讀更多精彩內容

  • 之前的JavaScript繼承一文中已經(jīng)介紹了繼承,但那篇只能算簡介。本篇結合原型鏈詳細介紹一下JavaScrip...
    張歆琳閱讀 2,690評論 0 8
  • 我是誰,我來自哪,我是誰的誰 想必大家一定在學習或者開發(fā)過程常常被JS獨有的原型繼承撥過不少腦弦吧,為何不迎問題而...
    俗三瘋閱讀 395評論 0 2
  • 現(xiàn)在有一個"動物"對象的構造函數(shù),還有一個"貓"對象的構造函數(shù)。 怎樣才能使"貓"繼承"動物"呢? 一、 構造函數(shù)...
    wavesnow閱讀 380評論 0 1
  • 例子 我們生成兩個構造函數(shù),后面的例子都是讓‘’貓‘’繼承‘’動物‘’的所有屬性和方法。 動物(為了更好的理解各種...
    流光號船長閱讀 376評論 0 1
  • 文/雪中萍 昨天,七節(jié)“生命樹”微學院家庭教育課結束,有點兒過去上學放假的感覺——輕松,看看所記筆記和作業(yè),很有...
    雪中萍閱讀 7,570評論 8 11

友情鏈接更多精彩內容