JS-继承(es5,es6)

首先须要了解原型链机制: 原型链做为实现继承的主要方法,其基本思想就是利用原型让一个引用类型继承另 一个引用类型的属性和方法.函数

构造函数、原型、实例之间的关系: 每一个构造函数都有一个原型对象(prototype),原型对象都包含一个指向构造函数的指针(constructor),而实例都包含一个指向原型对象的内部指针(__propto__).this

自我理解: 其实每一个Function都是Object基类的一个实例,因此每一个Function上都有一个__proto__指向了Object.prototype.当查找一个实例的属性时,会先从这个实例的自定义属性上找,若是没有的话经过__proto__去实例所属类的原型上去找,若是尚未的话再经过原型(原型也是对象,只要是对象就有__proto__属性)的__proto__到Object的原型上去找,一级一级的找,若是没有就undefined(Object的__proto__返回undefined).prototype

(一) 原型链继承 :

function Parent(name) { 
    this.name = name;
 }
 Parent.prototype.printName = function() {
    console.log('parent name:', this.name);
}
function Child(name) {
    this.name = name;
}
Child.prototype = new Parent('father');
Child.prototype.constructor = Child;//因为Child.prototype继承Parent,致使constructor丢失
Child.prototype.printName = function() {
    console.log('child name:', this.name);
}
var child = new Child('son');
child.sayName();    // child name: son

这种方法存在两个缺点:指针

1.子类型没法给超类型传递参数;
2.Child.prototype.sayName 必须写在 Child.prototype = new Parent('father'); 
以后,否则就会被覆盖掉。
(二) 类式继承:

function Parent(name) { 
    this.name = name;
 }
 Parent.prototype.printName = function() {
    console.log('parent name:', this.name);
 }
 Parent.prototype.doSomthing = function() {
    console.log('parent do something!');
 }
function Child(name, parentName) {
    Parent.call(this, parentName);
    this.name = name;
}
Child.prototype.printName = function() {
    console.log('child name:', this.name);
}
var child = new Child('son');
child.printName();   // child name: son
child.doSomthing();   // TypeError: child.doSomthing is not a function

至关于 Parent 这个函数在 Child 函数中执行了一遍,而且将全部与 this 绑定的变量都切换到了 Child 上,这样就克服了第一种方式带来的问题。
缺点:没有原型,每次建立一个 Child 实例对象时候都须要执行一遍 Parent 函数,没法复用一些公用函数。code

(三) 组合式继承:前两种方式的结合

function Parent(name) {

    this.name = name;
}

Parent.prototype.printName = function() {
    console.log('parent name:', this.name);
}
Parent.prototype.doSomething = function() {
    console.log('parent do something!');
}
function Child(name, parentName) {
    Parent.call(this, parentName);// 第二次调用
    this.name = name;
}

Child.prototype = new Parent();// 第一次调用      
Child.prototype.constructor = Child;
Child.prototype.printName = function() {
    console.log('child name:', this.name);
}

var child = new Child('son');
child.printName();    // child name: son
child.doSomething();   // parent do something!

组合式继承是比较经常使用的一种继承方法,其背后的思路是使用原型链实现对原型属性和方法的继承,而经过借用构造函数来实现对实例属性的继承。对象

这样,既经过在原型上定义方法实现了函数复用,又保证每一个实例都有它本身的属性。继承

组合式继承是 JS 最经常使用的继承模式,但组合继承使用过程当中会被调用两次:一次是建立子类型的时候,另外一次是在子类型构造函数的内部。原型链

第一次调用构造函数显然是没有必要的,由于第一次调用构造函数时候不须要函数内部的那些实例属性,这么写只是想得到其原型上的方法罢了,因此这时候你可能会这样写:get

Child.prototype = Parent.prototype;原型

这样写显然是不对的:
1.首先,你这样写的话至关因而子类和父类都指向同一个对象,这时候若是你添加了新的方法给 Child 但实际上 Parent 并不须要,至关于强行给 Parent 添加了一个未知的方法。
2.其次,仔细想一想,这样体现不出继承的多态性,好比此时子类想要重写父类的 getName 的方法,那么父类的方法也就会随之修改,这显然违背了多态性。

也就是说咱们第一次调用构造函数的时候,实际上是无论构造函数里面的内容,这是咱们能够new一个空函数,将其prototype指向Parent.prototype,代码以下:

(四) 寄生组合式继承:

function Parent(name) {
    this.name = name;
}
Parent.prototype.printName = function() {
    console.log('parent name:', this.name);
}

function Child(name, parentName) {
    Parent.call(this, parentName);  
    this.name = name;    
}

function inheritPrototype(Parent, Child) {
    Child.prototype = Object.create(Parent.prototype);   //修改
    Child.prototype.constructor = Child;
}

inheritPrototype(Parent, Child);
Child.prototype.printName = function() {
    console.log('child name:', this.name);
}
Child.prototype.constructor = Child;

var parent = new Parent('father');
parent.printName();    // parent name: father

var child = new Child('son', 'father');
child.printName();     // child name: son
(五) ES 6 继承:

class Parent {
    constructor(name) {
        this.name = name;
    }
    doSomething() {
        console.log('parent do something!');
    }
    printName() {
        console.log('parent name:', this.name);
    }
}

class Child extends Parent {
    constructor(name, parentName) {
        super(parentName);
        this.name = name;
    }
    printName() {
         console.log('child name:', this.name);
    }
}
const child = new Child('son', 'father');
child.printName();            // child name: son
child.doSomething();        // parent do something!
const parent = new Parent('father');
parent.printName();           // parent name: father
相关文章
相关标签/搜索