面向對象、原型鏈、繼承

1、建立對象的幾種方法

// 一、字面量
var o1 = {name:'lihaixing'};
var o11 = new Object({name:'lihaixing'});
// 二、構造函數
var M = function(){this.name='haixing'};
var o2 = new M();
// 三、基於原型
var P = {name:'haixing'};
var o3 = Object.create(P);
var o33 = Object.create(null);

2、原型鏈

圖片.png

var M = function () {
    this.name = 'haixing';
    // return {}
};
var o2 = new M();
console.log(M.prototype.constructor === M); // true
console.log(M.prototype === o2.__proto__); // true
console.log(M.prototype.__proto__ === Object.prototype); // true
console.log(M.__proto__ === Function.prototype); // true
console.log(Object.prototype.__proto__===null); // true

console.log(o2 instanceof M); // true 
console.log(M.prototype instanceof Object); // true 
console.log(o2 instanceof Object); // true  只要一條鏈上就能夠

3、繼承

一、藉助構造函數實現繼承

function Parent1 () {
    this.name = 'parent1'
    this.play = [1, 2, 3]
}

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

缺點:只是將Parents的this指向了Child1的實例,但Parents.prototype中的屬性卻沒法繼承下來函數

// 測試
Parent1.prototype.say=function(){
    console.log('Parent1 prototye')
}
var son1 = new Child1();
son1.say(); // Uncaught TypeError: son1.say is not a function

二、藉助原型鏈實現繼承

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

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

Child2.prototype = new Parent2();

缺點:雖然實現了繼承,可是父類實例後的數據倒是共用得,致使某個實例改變父親的數據,其它兒子也就共享了測試

// 測試
Parent2.prototype.say = function () {
    console.log('say')
}
var son2 = new Child2();
var son22 = new Child2();

son2.say(); // say

son2.play.push(4);
console.log(son22.play); // [ 1, 2, 3, 4 ]

三、結合以上兩種方法實現繼承

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

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

Child3.prototype = new Parent3()

缺點:雖然解決了各自的缺點,但父類執行了兩次,而且父類和子類還有相同的屬性this

四、父類的原型賦值給子類

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

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

Child4.prototype = Parent4.prototype

缺點:spa

  • Child4.prototype = Parent4.prototype 使得Child4和Parent4實例得構造器都指向了Parent4, 這明顯不是咱們想要的,咱們但願Child4.prototype.contructor仍然是Child4
  • 有人想到Child4.prototype.constructor = Child4將構造器再改回來,可是這樣Parent4.prototype.contructor成了Child4也不妥呀
// 測試
var son4 = new Child4();
// instanceof 取決於由誰建立的,同一條線均可
console.log(son4 instanceof Child4, son4 instanceof Parent4) // true true

// 構造器在原型上呢,取決於Child4.prototype.contructor
console.log(Child4.prototype.constructor, Parent4.prototype.constructor) // Parent4 Parent4

五、基於object.create

function Parent5 () {
    this.name = 'parent5'
    this.play = [1, 2, 3]
}

function Child5 () {
    Parent5.call(this)
    this.type = 'child5'
}


Child5.prototype = Object.create(Parent5.prototype);
Child5.prototype.constructor = Child5;

最後一個比較完美,不說了prototype

var son5 = new Child5();
console.log(son5 instanceof Child5, son5 instanceof Parent5); // true true
console.log(Child5.prototype.constructor,Parent5.prototype.constructor); // Child5 Parent5
相關文章
相關標籤/搜索