function a(){
this.testing = 'testing';
}
function b(){
}
b.prototype = new a();
console.log(b.testing);
控制檯顯示未定義,而不是「測試」。我究竟做錯了什麼?原型繼承。這個簡單的例子有什麼不對?
function a(){
this.testing = 'testing';
}
function b(){
}
b.prototype = new a();
console.log(b.testing);
控制檯顯示未定義,而不是「測試」。我究竟做錯了什麼?原型繼承。這個簡單的例子有什麼不對?
您還沒有創建'b'的實例。
var bInstance = new b();
console.log(bInstance.testing);
換句話說,原型的屬性只出現在b
類型的對象,而不是在b()
構造函數本身。
單詞Prototypal +1。 – corsiKa 2011-03-28 03:18:22