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—Working around by storing reference to functions

You can work around this problem by storing references to functions:

import js.Lib;
import js.Dom;
class Main
{
static var but1:js.Button;
static var but2:js.Button;
static var myHandler:js.Event->Void;
public static function main(): Void
{
but1 = cast Lib.document.getElementById("button1");
but2 = cast Lib.document.getElementById("button2");
myHandler = clickHandler;
untyped but1.addEventListener("click", myHandler);
but2.onclick = removeHandler;
}
public static function clickHandler(ev : js.Event)
{
js.Lib.alert("You've clcked.");
}
public static function removeHandler(ev:js.Event)
{
untyped but1.removeEventListener("click", myHandler);
}
}

What just happened?

Note the use of the ...

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