我在網頁中編寫不同的事件HTML上下文的解析器。如何訪問從子類型調用的超類型的js事件對象?
的基本結構是這樣的:
註冊事件,並將其鏈接到一個分析器功能。
elem.addEventListener(
'click',
(e) => {
let parser = new UiResultsItemInfoParser(e);
eventInfo = parser.evntInfo;
submitEvnt(eventInfo);
}
);
UiResultsItemInforParser
是子類型稱爲EvntParser
和e
超類型的明明是事件對象。
EvntParser
看起來是這樣的:
function EvntParser (e) {
this._evntInfo = {};
this._evntInfo.time = e.timeStamp.toString();
this._evntInfo.type = 'event';
}
EvntParser.prototype = {
set evntInfo(e) {
this._evntInfo.time = e.timeStamp.toString();
this._evntInfo.type = 'event';
},
get evntInfo() {
return JSON.stringify(this._evntInfo);
}
};
UiResultsItemInforParser
從EvntParser
衍生,看起來像這樣:
function UiResultsItemInfoParser(e) {
EvntParser.call(this, e);
}
UiResultsItemInfoParser.prototype = new EvntParser();
UiResultsItemInfoParser.prototype.constructor = UiResultsItemInfoParser;
不幸的是,我得到TypeError: e is undefined
在EvntParser
行this._evntInfo.time = e.timeStamp.toString();
在觸發事件和在addEventListener
中創建新的UiResultsItemInfoParser
對象。
爲什麼會這樣,我怎樣才能修復它在EvntParser
訪問事件對象的過程中創建對象?
謝謝!非常有趣,我從一本全新的參考書和初學者書中獲得了模式(https://www.amazon.de/JavaScript-Fortgeschrittene-Objektorientierung-funktionale-Programmierung/dp/3836238381)。這真是一個恥辱。 (注意:我使用僞類符號只是爲了得到一種我不太瞭解的原型邏輯的感覺,但無論如何thx!) –
@CutúChiqueño:哦,那**是一種遺憾。如果這本書有一個網站,也許你可以報告一個錯誤......呃,正如我所說的,其他一切都是正確的。 :-) –