Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Passing a reference to this current anonymous function

window.addEventListener('unload', function(e)
{
    MyClass.shutdown();
    window.removeEventListener('unload', /* how to refer to this function? */);
}, false);
like image 471
Pablo Avatar asked May 12 '10 05:05

Pablo


People also ask

Can you pass parameters to anonymous function JavaScript?

The syntax is simple: you can simply declare the anonymous function and make it execute by just calling it using the parenthesis at the end of the function. You can simply pass the parameters inside the immediate execution of the anonymous function as we have seen in the above example.

How do I run an anonymous function in JavaScript?

To turn a normal anonymous function into a self-executing function, you simply wrap the anonymous function in parentheses and add a set of parentheses and a semicolon after it. The benefit of using self-executing anonymous functions is that the variables you create inside of them are destroyed when the function exits.

What is anonymous function in JavaScript?

In JavaScript, an anonymous function is that type of function that has no name or we can say which is without any name. When we create an anonymous function, it is declared without any identifier. It is the difference between a normal function and an anonymous function.

How do we assign an anonymous function to a variable in JavaScript?

The () makes the anonymous function an expression that returns a function object. An anonymous function is not accessible after its initial creation. Therefore, you often need to assign it to a variable. In this example, the anonymous function has no name between the function keyword and parentheses () .


3 Answers

Name your function.

function f(e) {
   MyClass.shutdown();
   window.removeEventListener('unload', f);
}
window.addEventListener('unload', f, false);

Edit I think this will work too. Good point Kobi!

window.addEventListener('unload', function f(e)
{
    MyClass.shutdown();
    window.removeEventListener('unload', f);
}, false);
like image 157
ykaganovich Avatar answered Oct 14 '22 18:10

ykaganovich


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

like image 36
Germán Rodríguez Avatar answered Oct 14 '22 18:10

Germán Rodríguez


The callee property of the arguments object always refers to the called function:

window.addEventListener('unload', function(e)
{
    MyClass.shutdown();
    window.removeEventListener('unload', arguments.callee);
}, false);

See: MDC: callee

like image 26
gnarf Avatar answered Oct 14 '22 18:10

gnarf