Posted by Kromey at 2:01pm Oct 15 '09
You must sign in to send Kromey a message
You must sign in to send Kromey a message
are spectacularly useful, and once you get used to them (I'll admit, they're friggin' bizarre if your background is in a language that doesn't have 'em) you'll bemoan [insert your language of choice]'s lack of them!
Also, it's exceedingly rare when anonymous functions are actually needed in JavaScript - you can almost certainly replace a use of an anonymous function with a function handle instead.
Another also, if you truly do need an anonymous function (e.g. you need to take advantage of JavaScript's "encapsulation" or whatever it's called to get some variables in), use your anonymous function to simple call another function. I do this all the time:
setTimeout(function(){myFunction(foo,bar)},5000);
Doing this lets you pass parameters to your functions when the invocation of the anonymous function doesn't normally permit that.
(I hereby make no assertions that the above code snippet is syntactically accurate; it merely serves to demonstrate the concept.)
If you really want to bemoan a crappy implementation of anonymous functions, take one look at PHP's create_function function.
Also, it's exceedingly rare when anonymous functions are actually needed in JavaScript - you can almost certainly replace a use of an anonymous function with a function handle instead.
Another also, if you truly do need an anonymous function (e.g. you need to take advantage of JavaScript's "encapsulation" or whatever it's called to get some variables in), use your anonymous function to simple call another function. I do this all the time:
setTimeout(function(){myFunction(foo,bar)},5000);
Doing this lets you pass parameters to your functions when the invocation of the anonymous function doesn't normally permit that.
(I hereby make no assertions that the above code snippet is syntactically accurate; it merely serves to demonstrate the concept.)
If you really want to bemoan a crappy implementation of anonymous functions, take one look at PHP's create_function function.