3

first of all sorry if I'm not posting on the right place or if there's already a duplicate, but i don't know what to call this or how to search for it.

Can someone please explain to me what does the following code mean:

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

Why do I need to have (Boolean) after the function call ?

noisy(Boolean)(0);

What do i call this type of function call ?

Thanks in advance!

João Silva
  • 114
  • 10
  • 1
    *"Why do I need to have (Boolean) after the function call ?"* It's not *after* the function call. `noisy(Boolean)` *is* the function call. You are calling `noisy` passing `Boolean` as an argument. – Felix Kling Mar 15 '17 at 20:02

4 Answers4

5

This is tricky stuff. (Boolean) might throw you off here, but really it's just a regular old parameter. You could replace Boolean with any function, like doStuff, and it'd still work.

And yes, as you pointed out, this is a duplicate question. You're looking at something called "closure." Here's a fantastic answer from StackOverflow:

How do JavaScript closures work?

Community
  • 1
  • 1
Gabe Rogan
  • 3,343
  • 1
  • 16
  • 22
  • Thanks for your answer, it really helped me a lot, and i even discovered Im still not good enough at closures or else i would have known it was one. – João Silva Mar 15 '17 at 20:16
  • After reading the link you posted, Example 6 actually explains my doubt. Thanks a lot really! – João Silva Mar 15 '17 at 20:40
  • 1
    You're welcome! Perhaps I should also clarify that you are, in fact, also dealing with [higher-order functions](https://en.wikipedia.org/wiki/Higher-order_function) (see link for simple definition) in your example. An example of a higher-order function is a [callback function](http://www.learn-js.org/en/Callbacks) (see link for simple explanation). – Gabe Rogan Mar 15 '17 at 20:54
  • I knew it was a Higher Order Function because I'm reading Eloquent JavaScript and that's the Chapter name ahahah, thanks anyway. I still have a lot of doubts about JavaScript but i wan't to learn every little intricacy about it. – João Silva Mar 16 '17 at 01:23
2

"noisy" is a function that returns a function if you call it.

By passing Boolean into the function, Boolean is called like Boolean(0) which results in false since 0 is a falsy value.

Boolean is just a constructor that you can use to create booleans or to cast any value to a boolean.

Teemoh
  • 332
  • 2
  • 11
2

You are calling a function noisy(...) which is returning another function which it constructs using information from its parameters. The function call noisy(Boolean)(0); is actually two function calls and could (maybe should) be written as:

var noisyFunction = noisy(Boolean)
noisyFunction(0)

The fact that noisy() takes a function (Boolean is a function MDN). Has no real effect on the syntax being used here, it could take no arguments, or something less zany.

Your selection of the higher-order-functions tag really kind of sums it up. This is an example of programming with them. The fact that the value f is available to noisyFunction after noisy returns is the result of a closure (MDN). Some programmers might then describe this as, "using a closure", but it is not specific to the pattern shown.

le3th4x0rbot
  • 2,493
  • 23
  • 32
  • Thanks for your answer, similarly to the other answer you helped me a lot thanks for explaining to me that i could call the same function in multiple ways, now it really looks like the type of closures i'm used to seeing! – João Silva Mar 15 '17 at 20:17
1

You need to pass an f - it could be anything else than the Boolean function. Try

const increment = x => x+1;
const noisyInc = noisy(increment);
console.log(noisyInc(1));

In your original code, the intermediate variable is omitted - just like I could've written noisy(increment)(1) here.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • For what it's worth, it *could* be anything else. Using `Boolean` also works because `Boolean` will just convert (cast) values into a boolean (and return) when you use it without `new`. – Nebula Mar 15 '17 at 20:01
  • @towerofnix Yes, that's what `Boolean` does. – Bergi Mar 15 '17 at 20:03