工作生活

继承

2019-07-04  本文已影响0人  如你眉间山水

一、原型链

二、借用构造函数

三、组合继承

使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。组合继承避免了原型链和借助构造函数的缺陷,融合了它们的优点,成为JavaScript中最常用的继承模式。

    function SuperType (name) {
        this.name = name;
        this.colors = ['red', 'blue', 'green']
    }

    SuperType.prototype.sayName = function () {
        alert(this.name);
    }

    function SubType (name, age) {
        //继承属性
        SuperType.call(this, name);             //第二次调用 SuperType()

        this.age = age;
    }

    //继承方法
    SubType.prototype = new SuperType();        //第一次调用 SuperType()
    SubType.prototype.constructor = SubType;
    SubType.prototype.sayAge = function () {
        alert(this.age);
    }

    var instance1 = new SubType('Jack', 27);
    instance1.colors.push('black');
    console.log(instance1.colors);  // ["red", "blue", "green", "black"]
    instance1.sayName();  // Jack
    instance1.sayAge();  // 27


    var instance2 = new SubType('Tom', 29);
    console.log(instance2.colors);  // ["red", "blue", "green"]
    instance2.sayName();  // Tom
    instance2.sayAge();  // 29

注:组合继承最大的问题是无论什么情况下,都会调用两次超类型构造函数。

四、原型式继承

1)

     function object (o) {
         function F () {}
         F.prototype = o;
         return o;
     }

object ()函数内部,先创建了一个临时性的构造函数,然后将传入的对象作为这个构造函数的原型,最后返回了这个临时类型的一个实例。从本质上讲,object ()对传入其中的对象执行了一次浅复制。

    var person = {
        name: 'Nicholas',
        friends: ['Shelby', 'Court', 'Van']
    }

    var anotherPerson = object(person);
    anotherPerson.name = 'Greg';
    anotherPerson.friends.push('Rob');

    var yetAnotherPerson = object(person);
    yetAnotherPerson.name = 'Linda';
    yetAnotherPerson.friends.push('Barbie');

    console.log(person.friends)   // ["Shelby", "Court", "Van", "Rob", "Barbie"]

新对象将person作为原型,而person中包含引用类型值属性,所以person.friends被共享。

2)Object.create()方法规范化了原型式继承。这个方法接受两个参数,一个是用作新对象原型的对象,一个是(可选)为新对象定义额外属性的对象。

1、只传一个参数时,与object ()方法的行为相同。

    var person = {
        name: 'Nicholas',
        friends: ['Shelby', 'Court', 'Van']
    }

    var anotherPerson = Object.create(person);
    anotherPerson.name = 'Greg';
    anotherPerson.friends.push('Rob');

    var yetAnotherPerson = Object.create(person);
    yetAnotherPerson.name = 'Linda';
    yetAnotherPerson.friends.push('Barbie');

    console.log(person.friends)   // ["Shelby", "Court", "Van", "Rob", "Barbie"]

2、第二个参数与Object.defineProperties()方法的第二个参数格式相同:每个属性都是通过自己的描述符定义的。以这种方式指定的属性会覆盖掉原型对象上的同名属性。

    var person = {
        name: 'Nicholas',
        friends: ['Shelby', 'Court', 'Van']
    }
     var anotherPerson = Object.create(person, {
        name: {
            value: 'Greg'
        }
    });
     alert(anotherPerson.name)  // Greg

五、寄生式继承

六、寄生组合式继承

通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。普遍认为寄生组合式继承是引用类型最理想的继承范式。

    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);  // 创建对象
        prototype.constructor = SubType;  // 增强对象
        SubType.prototype = prototype;  // 指定对象
    }

第一步:创建超类型原型的一个副本。
第二步:为创建的副本添加constructor属性,从而弥补因重写原型而失去的默认的constructor属性。
第三步:将新创建的对象(即副本)赋值给子类型的原型。

    function inheritPrototype(subType, superType) {
        var prototype = object(superType.prototype);  // 创建对象
        prototype.constructor = SubType;  // 增强对象
        SubType.prototype = prototype;  // 指定对象
    }
    
    function SuperType (name) {
        this.name = name;
        this.colors = ['red', 'blue', 'green']
    }

    SuperType.prototype.sayName = function () {
        alert(this.name);
    }

    function SubType (name, age) {
        //继承属性
        SuperType.call(this, name);

        this.age = age;
    }

    inheritPrototype(SubType, SuperType);

    SubType.prototype.sayAge = function () {
        alert(this.age);
    }
上一篇 下一篇

猜你喜欢

热点阅读