3

I'm hitting an error in advanced compilation mode.

Uncaught TypeError: Object #<d> has no method 'attachEvent'

After some source map magic I figured that this is thrown from goog.events.listen call, where first argument is my custom object, inheriting goog.events.EventTarget.

This is in closures's source

goog.events.EventTarget.prototype.addEventListener = function(
    type, handler, opt_capture, opt_handlerScope) {
  goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
};

So this function ends up on prototype of my object, along with customEvent_ = true, then in 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);
}

(The last line is the one that throws)

Shouldn't this end up in stack overflow? Why is it going into else branch if my object inherits addEventListener from EventTarget? In simple compilation mode, everything works fine. How does this work, and why am I getting the error only in advanced compilation mode?

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
skrat
  • 5,518
  • 3
  • 32
  • 48

1 Answers1

1

Can you add some more code? What does your custom event look like? Basically, the default 'addEventListener' from JS is re-writen. IE doesn't implement addEventListener, but only attachEvent and the event object returned is not a normal event, but a goog.events.BrowserEvent.

On Advanced Compilation mode, the compiler flattens(minifies) the properties of all the objects, including the event object. The properties of your custom event might get flattened on Advanced compilation mode(this is most likely the case here) and for this reason attachEvent() doesn't exist in the prototype. it may have become aE() or something like that. Put some more code up before anyone is able to come up with real useful suggestions.

flavian
  • 28,161
  • 11
  • 65
  • 105