我试图执行一个示例以了解继承。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( );