• 重温Javascript(三)-继承


    继承


      1.原型链继承

      基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。让原型对象等于另一个类型的实例,此时的原型对象将包含一个指向另一个原型的指针,相应的,另一个原型中也包含着一个指向另一个构造函数的指针。层层递进,构成了实例与原型的链条。

    function SuperType(){
        this.property = true;
    }
    
    SuperType.prototype.getSuperValue = function(){
        return this.property;
    };
    
    function SubType(){
        this.subproperty = false;
    }
    
    //继承了SuperType
    SubType.prototype = new SuperType();
    
    SubType.prototype.getSubValue = function (){
        return this.subproperty;
    };
    var instance = new SubType();
    alert(instance.getSuperValue()); //true

      完整的原型链继承示意图

      

      原型链继承的问题

      仍然是原型中引用类型的问题

    function SuperType(){
        this.colors = ["red", "blue", "green"];
    }
    
    function SubType(){
    }
    
    //继承了SuperType
    SubType.prototype = new SuperType();
    var instance1 = new SubType();
    instance1.colors.push("black");
    alert(instance1.colors); //"red,blue,green,black"
    var instance2 = new SubType();
    alert(instance2.colors); //"red,blue,green,black"

      

      2.借用构造函数 

    function SuperType(){
        this.colors = ["red", "blue", "green"];
    }
    
    function SubType(){
        //继承了SuperType
        SuperType.call(this);
    }
    
    var instance1 = new SubType();
    instance1.colors.push("black");
    alert(instance1.colors); //"red,blue,green,black"
    
    var instance2 = new SubType();
    alert(instance2.colors); //"red,blue,green"

      

    function SuperType(name){
        this.name = name;
    }
    
    function SubType(){
        //继承了SuperType,同时还传递了参数
        SuperType.call(this, "Nicholas");
        //实例属性
        this.age = 29;
    }
    
    var instance = new SubType();
    alert(instance.name); //"Nicholas";
    alert(instance.age); //29

      还是和创建对象的构造函数模式一样的问题,无法公用prototype的方法

      3.组合继承

      将原型链和借用构造函数的技术组合到一起。通过原型链实现原型属性和方法的继承,通过借用构造函数实现对实例属性的继承。通过在原型上定义方法实现了函数复用,又能够保证每个实例都有自己的属性

    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;
    }
    
    //继承方法
    SubType.prototype = new SuperType();
    SubType.prototype.constructor = SubType;
    
    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

      

      组合继承最大的问题就是无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。子类型最终会包含超类型对象的全部实例属性,在调用子类型构造函数时如果有需要得重写这些属性

      4.组合寄生式继承  

    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; //指定对象
    }
    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);
    };

      对于SubType的原型也不会出现SuperType的实例属性

    引用:

    《JavaScript高级程序设计中文版》

  • 相关阅读:
    网页工具KOBAS进行KEGG富集分析
    Novel LncRNA的实时定量PCR引物设计教程
    Annotated LncRNA的实时定量PCR引物设计教程
    GO分析-GOseq的使用教程
    转录因子预测-oPOSSUM 3.0的使用教程
    miRNA结合位点预测软件RNAhybrid的使用教程
    关于win10用户名设置为中文导致Rstudio画图报错的解决方法
    edgeR之配对检验分析差异基因的使用教程
    51nod 1051最大子矩阵和
    51nod最大字段和(1049 1254)
  • 原文地址:https://www.cnblogs.com/dopeter/p/6660385.html
Copyright © 2020-2023  润新知