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;
    		  }
    

      

  • 相关阅读:
    [VC++]轻松搞VC之定时器(Timer)
    [VC++]VC中如何获得当前系统时间
    [VC++]如何利用this获得窗口句柄
    SMART原则
    SQL配置
    术语百科
    关于SQL锁问题
    第六代OA办公理念(摘录)
    心动机型
    SQL2008R2的索引重建
  • 原文地址:https://www.cnblogs.com/zhangxiaofei/p/7496491.html
Copyright © 2011-2022 走看看