O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—Using DOM level 2 events

In level 2, there is a function named removeEventListener (detachEvent is the equivalent of it in Internet Explorer's model).

This function allows one to remove an event listener. It only needs the type of the event (as a string) and the listener to be removed.

Unfortunately, every time you pass a reference to a function, the haXe compiler creates a closure. Closures are like functions enclosing functions and as such, the reference you pass to addEventListener and the one you pass to removeEventListener will never be equal, resulting in the removeEventListener function not being able to remove the listener.

This can be tested in a very simple way.

Create an HTML file with the markup for two buttons and load ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required