JavaScript - Passing a reference to this current anonymous function

前端 未结 4 1100
忘掉有多难
忘掉有多难 2021-02-05 08:19
window.addEventListener(\'unload\', function(e)
{
    MyClass.shutdown();
    window.removeEventListener(\'unload\', /* how to refer to this function? */);
}, false);
         


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 09:25

    Howto use recursion on Anonymous Functions

    Lets say we have an anonymous factorial function and we want to do it recursively. How do we call a function without a name? Well in Javascript the arguments.callee property contains a pointer to the currently executing function which means an anonymous function can, indeed, call itself.

    alert((function(n){ if(n <= 1){return 1;}else{return n*arguments.callee(n-1);}})(10));
    

    source: http://www.hunlock.com/blogs/Functional_Javascript

提交回复
热议问题