JavaScript继承的几种方式

<html>
	<head>
    	<title>object-extend</title>
    	<script type="text/javascript">
        var instance1 = null;
        var instance2 = null;
        /*
        //1.extend from prototype chain, using this pattern there is a problem when there is a refrence type in the super object, all the son instances will share this refrence type attribute
        function SuperType(){
          this.property = true;
          this.colour = ['green','red'];
        }
        SuperType.prototype.getSuperValue = function(){
          return this.property;
        };
        function SubType(){
          this.subProperty = false;
        }
        SubType.prototype = new SuperType();
        SubType.prototype.getSubValue = function(){
          return this.subProperty;
        };
        instance1 = new SubType();
        alert(instance1.getSuperValue());
        alert(instance1.colour);
        instance2 = new SubType();
        alert(instance2.getSuperValue());
        alert(instance2.colour);
        instance1.colour.push('black');
        alert(instance1.colour);//green,red,black
        alert(instance2.colour);//green,red,black
        
        //2. constructor stealing, the son object can not call the method in parent's prototype's method and all the method should be define in the constructor that all the instance can not share the method, we will have a waste of memory
        function SuperType(){
          this.colour = ['red','black'];
          
        }
        SuperType.prototype.sayColour = function(){
            alert(this.colour);
          };
        function SubType(){
          SuperType.call(this);
        }
        instance1 = new SubType();
        alert(instance1.colour);
        instance2 = new SubType();
        alert(instance2.colour);
        instance1.colour.push('blue');
        alert(instance1.colour);//red,black,blue
        alert(instance2.colour);//red,black
        //instance1.sayColour(); //there will be an error
        alert(SubType.constructor);
        

        //3.conbination inheritance, everything goes well
        function SuperType(){
          this.name = 'super';
          this.colour = ['red','black'];
        }
        SuperType.prototype.sayColour = function(){
          alert(this.colour);
        };
        function SubType(){
          SuperType.call(this);
          this.age = 21;
        }
        SubType.prototype = new SuperType();
        SubType.prototype.sayAge = function(){
          alert(this.age);
        };

        instance1 = new SubType();
        instance1.sayColour();
        instance2 = new SubType();
        instance2.sayColour();
        instance1.colour.push('blue');
        instance1.sayColour();//red,black,blue
        instance2.sayColour();//red,black
        instance2.sayAge();//21
        */
        //4.parasitic combination, using this pattern we can just get the prototype and not to initial the other attribute
        function objct(o){
          function F(){};
          F.prototype = o;
          return new F();
        }
        function inheritPrototype(subType,superType){
          var prototype = objct(superType.prototype);
          prototype.constructor = subType;
          subType.prototype = prototype;
        }

        function SuperType(){
          this.name = 'super';
          this.colour = ['red','black'];
        }
        SuperType.prototype.sayColour = function(){
          alert(this.colour);
        };
        function SubType(){
          SuperType.call(this);
          this.age = 21;
        }
        inheritPrototype(SubType,SuperType);
        SubType.prototype.sayAge = function(){
          alert(this.age);
        };

        instance1 = new SubType();
        instance1.sayColour();
        instance2 = new SubType();
        instance2.sayColour();
        instance1.colour.push('blue');
        instance1.sayColour();//red,black,blue
        instance2.sayColour();//red,black
        instance2.sayAge();//21
    	</script>
	</head>
	<body>
    	
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值