我正在使用一個用於HTML5/CSS3/JS Web小部件遊戲的IIFE。爲此,我想創建專門的異常對象,它們是單個對象的後代。我在嘗試不可能的事情嗎?Javascript對象繼承與iife
到目前爲止的代碼是
window.Exception = (function(t) {
Exception.title = t;
Exception.msg = '';
function Exception(message) { this.msg = message; }
Exception.prototype = {
toString: function() { return this.title + ': ' + this.msg; }
};
return Exception;
})('Exception');
window.RefException = (function(parent, t) {
RefException.title = t;
RefException.msg = '';
function RefException(message) { this.msg = message; }
RefException.prototype = parent.prototype;
return RefException;
})(window.Exception, 'Reference Exception');
console.log((new RefException('blah')).toString());
當我看在控制檯,我得到undefined: blah
,所以RefException
在繼承原型的功能,而不是命名空間的變量title
。我錯過了什麼嗎?