Anonymous functions

Posted by Kromey at 2:01pm Oct 15 '09
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.
There are 2 private posts in this thread. You need to sign in to read them.

Below are the public posts you may view:

You currently have read-only access to this board. You must request an account to join the conversation.

Why Join 4thKingdom?

Note that there are no ads here. Just intelligent and friendly conversation. We keep the spam out, the trolls out, the advertisers out… 4K is just a low-key, old-fashioned site with members from around the world.
This community began in 1998, and we continue to accept new members today.

Hot Discussion Topics: