很好的一个例子,想了很久终于明白了原型和构造器:
这个例子中instance是subType的一个实例,所以instance包括一个subType的实例属性subProperty(false),instance的原型指向subType.prototype,subType.prototype中包括property(true)、getProperty(function),因为subType.prototype = new superType();这一句使得原型被重写所以没有constructor属性,而且重写后将superType的实例属性property赋值给subType.prototype,但getSubProperty是个原型方法所以仍然在superType中。
function superType(){
this.property = true;
}
superType.prototype.getSuperValue = function(){
return this.property;
}
function subType(){
this.subproperty = false;
}
subType.prototype.getSubValue = function(){
return this.subproperty;
}
subType.prototype = new superType();
var instance = new subType();
console.log(instance.getSuperValue());