【发布时间】:2013-11-04 14:12:39
【问题描述】:
我试图执行一个示例以了解继承。 B类继承自A。 成功执行的示例必须显示两个警报..
但是不工作.. 我以 MDN 为例..
代码如下
function A(a){
this.varA =a;
}
A.prototype={
varA:null,
doSomething:function(){
alert( "A invoked");
}
}
function B(a,b){
A.call(this,arguments);
this.varB = b;
}
B.prototype = Object.create(A.prototype,
varB : {
value: null,
enumerable: true,
configurable: true,
writable: true
},
doSomething:{
value:function(){
A.prototype.doSomething.apply(this,arguments);
alert("B invoked);
},
enumerable:true,
configurable:true,
writable:true
});
var a =new A(1);
a.doSomething( );
var b = new B(1,2);
b.doSomething( );
【问题讨论】:
-
B的构造函数应该使用A.apply而不是A.call。 -
@Matt 我改成 A.apply.. 但是还是不行,不行...jsfiddle.net/visibleinvisibly/LmUXw/30/..has 示例代码