javascript - Alternative to arguments.callee -
i have eventlistener listens entire document , records keystrokes, want remove listener when conditions met.
the following snippet of code:
document.addeventlistener('keyup', function(e) { var letter_entered = string.fromcharcode(e.keycode).tolowercase(); player.makeguess(letter_entered); if(player.win_status === true || player.lose_status === true) { document.removeeventlistener('keyup', arguments.callee, false); } });
this works, according mozilla developer docs method has been deprecated.
i'm aware can name function, is there alternative allow me continue using unnamed function?
use following process:
- create variable
- assign anonymous function variable
- invoke variable reference
- the anonymous function references using variable name
use such:
var foo = function(e) { "use strict"; console.log(e); document.removeeventlistener('keyup', foo, false); } document.addeventlistener('keyup', foo);
you can solve problem using y
combinator:
function y(f) { return function () { return f.bind(null, y(f)).apply(this, arguments); }; }
now can rewrite code follows:
document.addeventlistener("keyup", y(function (callee, e) { player.makeguess(string.fromcharcode(e.keycode).tolowercase()); if (player.win_status || player.lose_status) document .removeeventlistener("keyup", callee); }));
that's folks.
Comments
Post a Comment