zoukankan      html  css  js  c++  java
  • 寄生组合式继承

    实例1:

    function SuperType(name){
            this.name = name;
            this.colors = ['red','green','blue'];
        }
    
        SuperType.prototype.sayName = function(){
            console.log(this.name);
        }
        
        function SubType(name,age){
            SuperType.call(this,name);
            this.age = age;    
        }
        
        function inheritPrototype(SubType,SuperType){
            SuperType.prototype.constructor = SubType;//把上边的两个函数合并
            SubType.prototype = SuperType.prototype;
        }
        /**
            公用的构造函数
            function SubType(name,age){
                this.name = name;
                this.colors = ['red','green','blue'];
                this.age = age;    
            }
    
        */
        inheritPrototype(SubType,SuperType);
    
        SubType.prototype.sayAge = function(){
            console.log(this.age);
        }
    
        var p1 = new SubType('zhangsan',89);
        p1.sayName();
        p1.sayAge();
        console.log(p1.colors);
        console.log(p1);

    这个是利用call()和SuperType.prototype.constructor = SubType;//把上边的两个函数合并函数公用一个构造函数

  • 相关阅读:
    技术晨读_2015_11_29
    mysql的timeout
    Gradle目录解析
    flexbox简介
    elasticsearch 查询(match和term)
    内存那些事
    elasticsearch 文档
    elasticsearch 集群
    elasticsearch中的API
    小菜的程序员道路(三)
  • 原文地址:https://www.cnblogs.com/jokes/p/9280802.html
Copyright © 2011-2022 走看看