1:javascript的继承主要依靠原型链来实现,实现的本质是重写原型对象,代之以一个新类型的实例。
2:实现方法:
function SuperType(){
this.property = true;
}
SuperType.prototype.getSuperValue = function(){
return this.property;
}
function SubType(){
this.subproperty = false;
}
//继承了SuperType,没有使用SubType默认提供的原型,而是给他换了一个新原型,
//这个新原型就是SuperType的实例
Subtype.prototype = new SuperType();
var instance = new SubType();
alert(instance.getSuperValue()); //true
alert(instance instanceof Object ); //true
alert(instance instanceof SuperType ); //true
alert(instance instanceof SubType ); //true
alert(Object.prototype isPrototypeOf (instance)); //true
alert(SuperType.prototype isPrototypeOf (instance)); //true
alert(SubType.prototype isPrototypeOf (instance)); //true
3:此时instance.constructor指向的是SuperType,因为原来SubType.prototype指向了另一个对象——SuperType的原型,而这个原型对象的constructor属性指向aingSuperType。
4:所有函数的默认原型都是Object的实例,因此默认原型都会包含一个指针_proto_,指向Object.prototype。所以所有自定义类型都会继承toString(),valueOf()等默认方法的原因。
5: