zoukankan      html  css  js  c++  java
  • 面向对象----继承

    三、 组合继承 (原型链和借用构造函数的技术组合到一起) 

    ```javascript
    function SuperType(name){
    this.name = name;
    this.colors = ['red','yellow'];
    }
    
    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 a = new SubType("Alex",18);
    var b = new SubType("John",21);
    a.colors.push("black");
    console.log(a.colors)//['red','yellow','black'];
    a.sayName();//Alex
    a.sayAge();//18
    
    b.colors;//["red","yellow"];
    

      四、寄生组合式继承(集寄生式继承和组合继承的优点于一身,是实现基于类型继承的最有效方式)

    function SuperType(name){
    		  	 this.name = name;
    		  }
    		  
    		  SuperType.prototype.sayName = function(){
    		  	   alert(this.name);
    		  }
    		  
    		  function SubType(name,age){
    		  	   SuperType.call(this,name);
    		  	   this.age = age;
    		  }
    		  
    		  function object(o){
    		  	 function F(){};
    		  	 F.prototype = o;
    		  	 return new F();
    		  }
    		  
    		  function inheritProtyotype(sub,parent){
    		  	  var prototype = object(parent.prototype);
    		  	  prototype.constructor = sub;
    		  	  sun.prototype = prototype;
    		  }
    

      

  • 相关阅读:
    题解【DP100题1~10】
    新博客已建好!
    题解【语文1(chin1)- 理理思维】
    题解【[BJOI2012]算不出的等式】
    题解【[HAOI2006]受欢迎的牛】
    题解【[FJOI2018]所罗门王的宝藏】
    Redis常用命令
    mysql table 最新更新时间
    中国翻译史阶记
    HTTP Session原理
  • 原文地址:https://www.cnblogs.com/zhangxiaofei/p/7496491.html
Copyright © 2011-2022 走看看