Ad

Eloquent JavaScript:

- 1 answer

In Chapter 5 of the book Eloquent JavaScript, there's this example that I don't quite understand. It's the noisy function:

function noisy(f) {
  return function(arg) {
    console.log("calling with", arg);
    var val = f(arg);
    console.log("called with", arg, "- got", val);
    return val;
  };
}

noisy(Boolean)(0);
// → calling with 0
// → called with 0 - got false

When noisy is called, it has two arguments (Boolean)(0). How does that work? Can you call functions and place arguments in that manner? Any help is greatly appreciated, thanks!

Ad

Answer

Boolean is being passed into noisy as the parameter f. noisy itself returns a function and 0 is being passed into that returned function as the parameter arg.

Ad
source: stackoverflow.com
Ad