• JavaScript继承


    原型链继承

    为了让子类继承父类的属性(也包括方法),首先需要定义一个构造函数。然后,将父类的新实例赋值给构造函数的原型。代码如下:

    function Parent(){
            this.name = 'mike';
        }
    
        function Child(){
            this.age = 12;
        }
        Child.prototype = new Parent();//Child继承Parent,通过原型,形成链条
    
        var test = new Child();
        alert(test.age);
        alert(test.name);//得到被继承的属性
        //继续原型链继承
        function Brother(){   //brother构造
            this.weight = 60;
        }
        Brother.prototype = new Child();//继续原型链继承
        var brother = new Brother();
        alert(brother.name);//继承了Parent和Child,弹出mike
        alert(brother.age);//弹出12

    以上原型链继承还缺少一环,那就是Object,所有的构造函数都继承自Object。而继承Object是自动完成的,并不需要我们自己手动继承,那么他们的从属关系是怎样的呢?

    确定原型和实例的关系

    可以通过两种方式来确定原型和实例之间的关系。操作符instanceofisPrototypeof()方法:

    alert(brother instanceof Object)//true
    alert(test instanceof Brother);//false,test 是brother的超类
    alert(brother instanceof Child);//true
    alert(brother instanceof Parent);//true

    只要是原型链中出现过的原型,都可以说是该原型链派生的实例的原型,因此,isPrototypeof()方法也会返回true

    js中,被继承的函数称为超类型(父类,基类也行),继承的函数称为子类型(子类,派生类)。使用原型继承主要由两个问题:

    • 字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数。
    • 伪类解决引用共享和超类型无法传参的问题,我们可以采用“借用构造函数”技术

    借用构造函数(类式继承)

    function Parent(age){
            this.name = ['mike','jack','smith'];
            this.age = age;
        }
    
        function Child(age){
            Parent.call(this,age);
        }
        var test = new Child(21);
        alert(test.age);//21
        alert(test.name);//mike,jack,smith
        test.name.push('bill');
        alert(test.name);//mike,jack,smith,bill

    借用构造函数虽然解决了刚才两种问题,但没有原型,则复用无从谈起,所以我们需要原型链+借用构造函数的模式,这种模式称为组合继承

    组合继承

    function Parent(age){
            this.name = ['mike','jack','smith'];
            this.age = age;
        }
        Parent.prototype.run = function () {
            return this.name  + ' are both' + this.age;
        };
        function Child(age){
            Parent.call(this,age);//对象冒充,给超类型传参
        }
        Child.prototype = new Parent();//原型链继承
        var test = new Child(21);//写new Parent(21)也行
        alert(test.run());//mike,jack,smith are both21

      组合式继承是比较常用的一种继承方法,其背后的思路是 使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样,既通过在原型上定义方法实现了函数复用,又保证每个实例都有它自己的属性。

    原型式继承

    这种继承借助原型并基于已有的对象创建新对象,同时还不用创建自定义类型的方式称为原型式继承

    function obj(o){
             function F(){}
             F.prototype = o;
             return new F();
         }
        var box = {
            name : 'trigkit4',
            arr : ['brother','sister','baba']
        };
        var b1 = obj(box);
        alert(b1.name);//trigkit4
    
        b1.name = 'mike';
        alert(b1.name);//mike
    
        alert(b1.arr);//brother,sister,baba
        b1.arr.push('parents');
        alert(b1.arr);//brother,sister,baba,parents
    
        var b2 = obj(box);
        alert(b2.name);//trigkit4
        alert(b2.arr);//brother,sister,baba,parent

    原型式继承首先在obj()函数内部创建一个临时性的构造函数 ,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的一个新实例。

    寄生式继承

    这种继承方式是把原型式+工厂模式结合起来,目的是为了封装创建的过程。

     function create(o){
            var f= obj(o);
            f.run = function () {
                return this.arr;//同样,会共享引用
            };
            return f;
        }

    组合式继承的小问题

    组合式继承是js最常用的继承模式,但组合继承的超类型在使用过程中会被调用两次;

    • 一次是创建子类型的时候,
    • 另一次是在子类型构造函数的内部
    function Parent(name){
            this.name = name;
            this.arr = ['哥哥','妹妹','父母'];
        }
    
        Parent.prototype.run = function () {
            return this.name;
        };
    
        function Child(name,age){
            Parent.call(this,age);//第二次调用
            this.age = age;
        }
    
        Child.prototype = new Parent();//第一次调用

    以上代码是之前的组合继承,那么寄生组合继承,解决了两次调用的问题。

    寄生组合式继承(最理想的继承模式)

      寄生式组合继承:通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。

      基本思路是:不必为了指定子类型的原型而调用超类型的构造函数,我们所需要的无非就是超类型原型的一个副本而已。本质上,使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。

      基本模式:

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

      这个示例中的inheritPrototype()函数实现了寄生组合式继承的最简单形式。

      这个函数接收两个参数:子类型构造函数和超类型构造函数。

      在函数内部:

      第一步是创建超类型原型的一个副本。

      第二步是为创建的副本添加constructor属性,从而弥补因重写原型而失去的默认的constructor属性。

      最后一步,将新创建的对象赋值给子类型的原型。

    这样,我们就可以使用inheritPrototype()函数的语句,去替换前面例子中为子类型原型赋值的语句了,如:

    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);
    }
    
    var instance1 = new SubType("Nicholas", 29);
    instance1.colors.push("black");
    alert(instance1.colors);  //"red,blue,green,black"
    instance1.sayName();      //"Nicholas"
    instance1.sayAge();       //29
    
    var instance2 = new SubType("Greg", 27);
    alert(instance2.colors);  //"red,blue,green"
    instance2.sayName();      //"Greg"
    instance2.sayAge();       //27

      这个例子的高效率体现了它只调用了一次SuperType构造函数,并且因此避免了在SubType.prototype上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用instanceof和isPrototypeOf()。开发人员普遍认为寄生组合式继承是引用类型最理想的继承模式。

      部分来源于JavaScript高级程序设计!若有不当之处,望不吝赐教!

  • 相关阅读:
    Single Image Haze Removal Using Dark Channel Prior翻译
    android的单元测试
    HTML&JS笔记(1)
    动手学Javascript(1)——PopStar
    Cocos2d-x教程第(11)讲-利用遮罩(蒙版)CCLayerColor制作新手引导界面(上)
    硬币翻转倍数递增试算
    java泛型接口详解
    1352
    大话设计模式之——简单工厂模式
    iOS自适应行高方法及问题
  • 原文地址:https://www.cnblogs.com/puyongsong/p/5976324.html
Copyright © 2020-2023  润新知