关于一个原型链例子的问题,菜鸟求解function SuperType(){this.propertytrue//为什么需要这行代码,它有
关于一个原型链例子的问题,菜鸟求解
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;
};
急求各位高手答疑解惑!!!! javascript
[解决办法]
就是拿来对比的
[解决办法]
function SuperType(){
this.property=true;//为什么需要这行代码,它有什么作用啊????
//给SuperType的实例添加一个属性,属性名叫property,值为true
}
function SubType(){
this.subproperty=false;//这行代码有什么作用呢????
//给SubType的实例添加一个属性,属性名叫subproperty,值为true
}
执行一下下面就知道了。然后注释那句后再执行一下。
var superType = new SuperType();
alert(superType.property);
[解决办法]
比如讲
var a = new SuperType(); 那么a应该是这样的:
a {
property, --从SuperType构造函数那得来的
getSuperValue --从SuperType的原型那得来的
}
如果让SubType.prototype=new SuperType(); 上面那个a是一个道理:
SubType.prototype {
property, --从SuperType构造函数那得来的
getSuperValue --从SuperType的原型那得来的
}
因为这个是SubType的原型,所以如果
var b = new SubType()
b里面也就有了那些属性。这就是继承。
[解决办法]
var inst1 = new SuperType(); // inst1.property == true;
// IF
function SuperTypeOpt() {
var property = true;
}
var inst2 = new SuperTypeOpt(); // inst2.property == undefined
