最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Alternative to arguments.callee - Stack Overflow

programmeradmin0浏览0评论

I have an EventListener that listens to the entire document and records keystrokes, but I want to remove this Listener when certain conditions are met.

The following is a snippet of my 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, however according to the Mozilla Developer Docs this method has been deprecated.

I'm aware that I can simply name the function, but is there an alternative that would allow me to continue using the unnamed function?

I have an EventListener that listens to the entire document and records keystrokes, but I want to remove this Listener when certain conditions are met.

The following is a snippet of my 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, however according to the Mozilla Developer Docs this method has been deprecated.

I'm aware that I can simply name the function, but is there an alternative that would allow me to continue using the unnamed function?

Share Improve this question edited Jan 28, 2015 at 14:51 leppie 117k18 gold badges200 silver badges300 bronze badges asked Oct 6, 2013 at 23:05 MitulP91MitulP91 1,3252 gold badges13 silver badges25 bronze badges 5
  • No. You have to store a reference to it somehow, like arguments.callee does. – Joe Simmons Commented Oct 6, 2013 at 23:09
  • Why would you insist on using an unnamed function? – user2736012 Commented Oct 6, 2013 at 23:10
  • @user2736012 Question is more out of curiosity than practical use. – MitulP91 Commented Oct 6, 2013 at 23:12
  • No, there's no other way other than to use a variable reference to an anonymous function. If there was, it would just be redundant. – user2736012 Commented Oct 6, 2013 at 23:13
  • possible duplicate of Arguments.callee is deprecated - what should be used instead? – DavidRR Commented Jun 18, 2015 at 19:24
Add a comment  | 

2 Answers 2

Reset to default 16 +100

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.

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

  • Strict Mode is Coming to Town

  • ES3.1:arguments.callee

  • Recursion with anonymous (inline) functions in XPath 3.0

  • Recursion with anonymous (inline) functions in XPath 3.0, Part II

发布评论

评论列表(0)

  1. 暂无评论