我在高级编译模式下遇到错误。
Uncaught TypeError: Object #<d> has no method 'attachEvent'
经过一些源映射魔术后,我发现这是从goog.events.listen
调用中抛出的,其中第一个参数是我的自定义对象,继承goog.events.EventTarget
.
这是在闭包的来源
goog.events.EventTarget.prototype.addEventListener = function(
type, handler, opt_capture, opt_handlerScope) {
goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
};
所以这个函数最终在我的对象的原型上,以及customEvent_ = true
,然后在goog.events.listen
// Attach the proxy through the browser's API
if (src.addEventListener) {
if (src == goog.global || !src.customEvent_) {
src.addEventListener(type, proxy, capture);
}
} else {
// The else above used to be else if (src.attachEvent) and then there was
// another else statement that threw an exception warning the developer
// they made a mistake. This resulted in an extra object allocation in IE6
// due to a wrapper object that had to be implemented around the element
// and so was removed.
src.attachEvent(goog.events.getOnString_(type), proxy);
}
(最后一行是抛出的那一行)
这不应该最终导致堆栈溢出吗?else
如果我的对象继承addEventListener
自,为什么它会进入分支EventTarget
?在简单编译模式下,一切正常。这是如何工作的,为什么我只在高级编译模式下收到错误?