我看到了很多這樣的代碼:公約原型繼承在JavaScript
function Base() {}
function Sub() {}
Sub.prototype = new Base();
但是,如果你這樣做:
s = new Sub();
print(s.constructor == Sub);
這是錯誤的。這似乎讓我感到困惑,因爲s的構造函數實際上是Sub。這是常規/更好的做法嗎?
function Base() {}
function Sub() {}
Sub.prototype = new Base();
Sub.prototype.constructor = Sub;
還是沒有關係?
我剛剛通過一個HTML頁面作爲警報(s.constructor == Sub)運行比較,並且它返回了真實的結果 – 2008-12-31 18:13:38
許多框架調整`constructor`屬性爲正確地指向子類的構造函數,我的帖子在上面顯示的代碼中處理了這個問題和其他問題。http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html – 2012-05-15 00:49:13