window.addEventListener('unload', function(e)
{
MyClass.shutdown();
window.removeEventListener('unload', /* how to refer to this function? */);
}, false);
-
1please improve question's title for later searching – Matthew Lock May 12 '10 at 05:06
4 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);

- 14,736
- 8
- 59
- 96
-
well I'm aware of that one, but I don't want to change anonymous function to named... I like the way it is now. – Pablo May 12 '10 at 05:09
-
3@Michael - the function can be named locally, within its closure. It's a small change. – Kobi May 12 '10 at 05:13
-
3@Michael: Naming your function is *by far* the best way to allow yourself to call it. Using `arguments.callee` is dramatically slower on most current implementations (between 2x and 10x -- yes, really), and won't work with the new "strict mode" of ECMAScript (which you may find you want to start using at some point). Now, the performance aspect doesn't matter for unhooking an `onload` handler, but just pointing that out in case you apply the pattern elsewhere. – T.J. Crowder May 12 '10 at 05:37
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));

- 4,284
- 1
- 19
- 19
-
2so it would be just `window.removeEventListener('unload',arguments.calee)` ? – Pablo May 12 '10 at 05:07
-
@Michael: With two "l"s, but yes. But in *general* it's not a good way to do it; see my comment on ykaganovich's answer. – T.J. Crowder May 12 '10 at 05:38
-
1This is the answer! I really wish this would be chosen instead of the one that is currently. – Bruno Bronosky Feb 22 '13 at 07:47
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

- 105,192
- 25
- 127
- 161
I haven't tried this, but how about moving the removeEventListener method call into MyClass itself. The method won't be anonymous, but you won't be polluting the global namespace and it will be part of the class it's manipulating. You can even make it "private". I'm not sure what your style is, but I'd write it something like this:
var MyClass = function(){
var self = this;
self.shutdown = function(){
window.removeEventListener('unload',self.shutdown,false);
};
self.initialize = function() {
window.addEventListener('unload',self.shutdown,false);
};
return self;
};
var myObject = new MyClass();
myObject.initialize();
I guess it depends on what MyClass does and how you use it.

- 14,204
- 15
- 60
- 104