標籤:code 技術分享 class 函數傳參 let log 組合 .com 重複
提到繼承,我們常常會聯想到C#、java等物件導向的進階語言(當然還有C++),因為存在類的概念使得這些語言在實際的使用中抽象成為一個對象,即物件導向。JavaScript這門語言本身就是作為瀏覽器指令碼語言的弱語言,伴隨著沒有類的概念,JavaScript就成為了一種基於對象的語言而不是物件導向的語言,物件導向就會存在繼承,那麼基於對象的JavaScript是如何繼承的,老生常談一下。
(1)原型繼承
function Parent() { this.name = "dqhan"; this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘]; this.action = function () { console.log(‘Iamdqhan‘); };}function Child() { //do someting}Child.prototype = new Parent(); //markvar child1 = new Child();var child2 = new Child();
原型繼承是最基礎的繼承方式,核心就是重寫子類原型,是父類執行個體對象充當子類原型,
優點:通俗易懂
缺點:(1)原型中的參考型別共用(2)無法向父類建構函式傳參
child1.arr.push(‘houhou‘);
console.log(child1.arr); // ["haha", "heihei", "hehe", "houhou"]
console.log(child2.arr); // ["haha", "heihei", "hehe", "houhou"]
原因就是操作child1中的arr時,首先回到child1下尋找arr,當沒有時會在child1的原型裡尋找,此時arr為child1原型中的屬性,由於child2與child1的原型同為Parent的執行個體,此時更改arr值時,child2也會變,即原型共用。
那麼如果現在更改屬性name呢?
child1.name = "newdqhan";
console.log(child2.name); // "dqhan"
這又是為什麼呢?因為child1.name會直接在child下重新定義一個name屬性,這個name並不是原型上的屬性,也就不會更改原型,。
(2)借用建構函式實現繼承
function Parent(value) { this.name = "dqhan"; this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘]; this.value = value; this.action = function () { console.log(‘Iamdqhan‘); };}function Child(value) { Parent.call(this, value); //mark}var child1 = new Child("value1");var child2 = new Child("value2");
child1.action === child2.action //false
看一下結果
利用Call的方式將Child方法中的this傳入Parent方法中,然後重新定義一遍Parent中的屬性及方法
Call方法我們這個換一種寫法:
function Child(value) { this.tempParent = Parent; this.tempParent(value); delete this.tempParent}
在Child函數中引用Parent建構函式並執行,此時就會在Child中定義了一遍Parent中的屬性及方法。看到了這裡我相信你一定會產生一個疑惑就是為什麼不在Child中直接執行一次Parent函數呢。
看一下結果
並沒有達到理想的結果,這是為啥呢?因為在Child中不引用Parent,直接調用,Parent中的this預設為Window對象(嚴格開發模式下為Undefined),目的就是為了傳遞this。
function Parent() { this.name = "dqhan"; //this 為 Window 對象 this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘]; this.action = function () { console.log(‘Iamdqhan‘); }; } function Child(){ Parent(); } var child = new Child();
function Parent() {
"use strict"; this.name = "dqhan"; //this 為 undefined this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘]; this.action = function () { console.log(‘Iamdqhan‘); }; } function Child(){ Parent(); } var child = new Child();
優點:摒棄了原型,避免了原型共用;解決了向父類建構函式傳參的問題。
缺點:沒產生一個新對象,都會重新定義一次function,嚴重影響記憶體。
(3)組合繼承:
這種繼承方式相比第二種將父類方法中的函數定義在了原型內
function Parent() { this.name = "dqhan"; this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘];}Parent.prototype.action = function () { console.log(‘Iamdqhan‘);};function Child() { Parent.call(this);}
Child.prototype = new Parent();var child1 = new Child("value1");var child2 = new Child("value2");child1.action === child2.action // true
結果:
優點:這種方式成功的避免了重複定義function的尷尬情況,同時解決了原型共用的問題。
缺點:如果有兩個子類繼承父類,但是父類的屬性有一個子類不用,怎麼搞?這個是沒法避免的,而且父類的屬性全部在子類的原型上,很不美觀。
(4)寄生組合繼承:
為了扣掉組合繼承中原型中不需要的屬性,看到為了滿足這一點,可不可以介樣:
function Parent() { this.name = "dqhan"; this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘];}Parent.prototype.action = function () { console.log(‘Iamdqhan‘);};function Child() { Parent.call(this);};Child.prototype = Parent.prototype;
結果:
乍一看好像對,實際Child中的__proto__為Object,並不是Parent,已經背離了Child繼承Parent的目的。
改進:
function Parent() { this.name = "dqhan"; this.arr = [‘haha‘, ‘heihei‘, ‘hehe‘];}Parent.prototype.action = function () { console.log(‘Iamdqhan‘);};function Child() { Parent.call(this);};function initObject(obj) { var F = function () { }; F.prototype = obj; return new F();}//Child.prototype = Parent.prototype;var newPrototype = initObject(Parent.prototype);newPrototype.constructor = Child;Child.prototype = newPrototype;var child1 = new Child("value1");var child2 = new Child("value2");
結果:
Child的__proto__已經為Parent,而且去掉了原型上我們不要的屬性只留下父類在原型上定義的方法,核心目的就是為了更改原型鏈的設定。
淺談JS中的繼承