[英]Closure Compiler, missing method with advanced optimizations?
我在angularjs應用程序中使用閉包編譯器。 我的JS編譯時沒有錯誤(或警告),並且可以通過SIMPLE優化正常工作。 具體來說,我啟用了以下警告/檢查:
--jscomp_warning=checkTypes \
--jscomp_error=checkVars \
--jscomp_warning=deprecated \
--jscomp_error=duplicate \
--jscomp_warning=globalThis \
--jscomp_warning=missingProperties \
--jscomp_warning=undefinedNames \
--jscomp_error=undefinedVars \
但是,當我嘗試使用ADVANCED OPTIMIZATIONS進行編譯時,出現以下錯誤:
TypeError: a.handleEvent is not a function
at Sj.Nh.a.(anonymous function).a.(anonymous function) (http://localhost:10080/main/pattern.dots-0-7-7-258310cc.compiled.js:118:273)
at $h (http://app.js:120:424)
at R (http://app.js:119:337)
at lj (http://app.js:144:380)
at Sj.f.re (http://app.js:151:622)
at mo (http://app.js:302:171)
at to (http://app.js:316:78)
at link (http://app.js:308:335)
...
它似乎與事件處理代碼有關(通過查看源映射):
/**
* @param {Object|Function} listener The listener function or an
* object that contains handleEvent method.
* @return {!Function} Either the original function or a function that
* calls obj.handleEvent. If the same listener is passed to this
* function more than once, the same function is guaranteed to be
* returned.
*/
goog.events.wrapListener = function(listener) {
goog.asserts.assert(listener, 'Listener can not be null.');
if (goog.isFunction(listener)) {
return listener;
}
goog.asserts.assert(
listener.handleEvent, 'An object listener must have handleEvent method.');
if (!listener[goog.events.LISTENER_WRAPPER_PROP_]) {
listener[goog.events.LISTENER_WRAPPER_PROP_] =
function(e) { return listener.handleEvent(e); };
}
return listener[goog.events.LISTENER_WRAPPER_PROP_];
};
但這似乎真的很奇怪。 畢竟,有一個斷言(顯然)通過了一對聲明。 我想我已經嘗試保留斷言( -D goog.asserts.ENABLE_ASSERTS
),即使斷言被優化了,我也不明白為什么它可以與SIMPLE優化一起工作(斷言仍然存在)。 另外,如果我使用高級優化和--debug
編譯,代碼仍然可以正常工作,這看起來像是開始了命名空間崩潰的過程,但並沒有完全結束。
有趣的是,如果我嘗試添加一些console.log
語句:
goog.events.wrapListener = function(listener) {
goog.asserts.assert(listener, 'Listener can not be null.');
if (goog.isFunction(listener)) {
return listener;
}
goog.asserts.assert(
listener.handleEvent, 'An object listener must have handleEvent method.');
if (!listener[goog.events.LISTENER_WRAPPER_PROP_]) {
listener[goog.events.LISTENER_WRAPPER_PROP_] =
function(e) {
console.log(e);
console.log(listener);
console.log(typeof listener);
console.log(listener.handleEvent);
console.log(typeof listener.handleEvent);
return listener.handleEvent(e);
};
}
return listener[goog.events.LISTENER_WRAPPER_PROP_];
};
而且我看到typeof listener
是'function'
。 但是,如果真是這樣,我們首先是如何到達這里的? (當然goog.isFunction(listener)
應該在那時返回true
了。)。
我對這里可能發生的事情有點不知所措...
好吧,似乎在嘗試調試了無數小時之后,答案再次出現在我的膝蓋上(再次)。
不幸的是,我無法將goog.isFunction
編譯為符號ga
,它與全局使用情況跟蹤庫“ Google Analytics(分析)”相沖突。
對我來說,解決方案是在我的外部環境中僅包含universal_analytics_api.js
,這一切似乎都與世隔絕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.