日期:2014-05-16  浏览次数:20358 次

javascript中的继承-原型链
function SuperType(){
      this.property=true;
}
SuperType.prototype.getSuperValue=function(){
      return this.property;
};

function SubType(){
      this.subproperty=false;
}
SubType.prototype=new SuperType(); //这里继承了SuperType
SubType.prototype.getSubValue=function(){
      return this.subproperty;
}
var instance=new SubType();
alert(instance.getSuperValue()); //true
复制代码

这里必须先撸一撸instance SubType SuperType之间的关系:

1.instance是SubType的实例

2.instance的原型是SuperType的实例,因为上面的代码没有使用SubType的默认原型

3.SuperType的原型对象为SuperType Prototype

4.SuperType Prototype的构造函数constructor指向SuperType的构造函数

总结起来就是:

1.instance指向SubType的原型

2.SubType的原型又指向SuperType的原型(因为构造函数肯定会有一个默认原型)