JS中实现继承

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();

 

2.第二种方式:借用构造函数

  funtion SuperType(){

       this.colors = ["red","blue","green"];

     }

     function SubType(){

    //继承了SuperType

    SuperType.call(this);

     }

 

posted @ 2013-10-26 18:32  wenwei  阅读(298)  评论(0编辑  收藏  举报