Alternative to arguments.callee

核能气质少年 提交于 2019-11-28 00:03:23
Aadit M Shah

Use the following process:

  • Create a variable
  • Assign an anonymous function to the variable
  • Invoke it with the variable reference
  • The anonymous function references itself using the variable name

Use it as such:

   var foo = function(e)
    {
    "use strict";
    console.log(e);
    document.removeEventListener('keyup', foo, false);
    }

document.addEventListener('keyup', foo);

You can solve this problem easily using the y combinator:

function y(f) {
    return function () {
        return f.bind(null, y(f)).apply(this, arguments);
    };
}

Now you can rewrite your code as 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 all folks.

Paul Sweatte

Use another anonymous function as a wrapper to store a named function (callee shim) to your original function:

document.addEventListener('keyup', (function(e)
  {
  var aFunction = function()
    {
    var letter_entered = String.fromCharCode(e.keyCode).toLowerCase();
    player.makeGuess(letter_entered);
    };

  if(player.win_status === true || player.lose_status === true) 
    {
    document.removeEventListener('keyup', window, false);
    }
  else
    {
    aFunction();
    }
  }
), false);

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!