[Javascript] Anonymous functions in parameter space

Mike Dougherty mdougherty at pbp.com
Wed Jul 20 12:19:58 CDT 2011


On Wed, Jul 20, 2011 at 1:03 PM, Paul M Foster <paulf at quillandmouse.com>wrote:

> Why is it common practice to write pass anonymouse functions as
> parameters to other functions in javascript? Example:
>
> function alfa(function(){ do_some_stuff; }){ do_other_stuff; };
>
> I see this a lot, and it's unbelievably confusing to decode/read. Why is
> it done this way? Instead, why not do it this way:
>
> function bravo() { do_some_stuff; };
>
> var charlie = bravo();
>
> function alfa( parm ) { do_other_stuff; };
>
> alfa(bravo);
>
> Any help?
>
> If you pass the function then in the code you have abstracted as
"do_some_stuff" the function can be used, perhaps multiple times.

In your rewrite you are evaluating the function and storing the result then
passing the result.

as to why it's common practice, I'm not sure.  Maybe there is a good reason
in each context.  A less-good reason is general programmer laziness to first
declare the function as a variable since it'll be re-referenced by the
interface to which it is passed.


More information about the Javascript mailing list