Using a function with parameters as parameter

Imagine that you have a function that expects a reference to a function. Here is an example of such a function:

function bar(fn) { fn(); }

Now imagine that the function that you want to pass to bar accepts a parameter. Here is an example of such a function:

function foo(arg) { alert(arg); }

With the help of a closure this is no problem:

bar(function e() { foo('hello'); });

PS: Kudos go to Weirdan for providing the solution to this problem.

This entry was posted on Tuesday, April 4th, 2006 at 02:46 and is filed under JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.