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.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>