我試圖執行一個樣本來理解繼承。 B類從A 成功執行的例子必須證明兩個警報繼承..用Object.create在Javascript中繼承
但不工作.. 我已經採取了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
@Matt我更改爲A.apply ..但仍然沒有,噸工作... http://jsfiddle.net/visibleinvisibly/LmUXw/30/..s示例代碼 – visibleinvisibly