It's just a higher-level function, which in this case isn't really necessary.
f
is a function that takes another function (called a
), and returns a newly generated function that will evaluate a
and pop up an alert box showing the result.
So the bottom line calls f (passing in an anonymous function that prints "Hello World"), then immediately evaulates the anonymous function returned by f
- which will evaluate the argument passed in (which you can see returns "Hello World") and then pops up an alert box.
The code posted is functionally equivalent to
alert("Hello World");
but there's two extra elements that make it more complex:
- You can pass in an arbitrary function in order to generate the string that appears in the alert box (and this will be lazily evaluated, which could be important - e.g. a function to print the current time/app status/memory use when the alert is shown rather than when the method was created).
- You can generate a closure that will show this alert and then pass it around, rather than the alert executing immediately.
But since neither of these benefits are actually used in the code snippet, I can see why you'd be confused.