I am reading Eloquent JavaScript (The new edition) and I reached the part on higher order functions and I'm confused on what's happening in the following code.
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 is the call to the function noisy like this? Is (Boolean) a cast? A cast for what? the return value? or the argument? why not (Boolean)noisy(0) if its the return value. Or noisy((Boolean) 0) if the argument is the one being casted.
noisy(Boolean)(0)
What is happening in this line? Where is f() even defined?
var val = f(arg);
A function without the () is the actual function. A function with () is an invocation of the function. Also keep in mind that JavaScript is a loosely typed language, so you don't declare variable types. I've added some comments to your example to try and help.
So then noisy(Boolean)(0) works like this
f is the function Boolean
noisy returns a function like this
So now we have
our returned function(0)
which executes like normal to become
Boolean
is a function. It's the function you're calling indirectly throughnoisy
. A bit confusing, I know, because it looks like the name of a type. But in JavaScript, those initially-capped things (Boolean
,Number
,String
, and so on) are functions. When you callBoolean
(without usingnew
), it tries to convert the argument you gave it into aboolean
primitive value and returns the result. (See §15.6.1 in the spec.)f
is the name of the argument in thenoisy
function.Functions in JavaScript are first-class objects. You can pass them into other functions as arguments just like any other object.
When you do
There are two things going on. First:
That gives us a function that, when called, will call
Boolean
with the argument we give it while also doing thoseconsole.log
statements. This is the function you see being created innoisy
(return function(arg)...
);Then we call that function:
And that's when you see the console output. Since
Boolean(0)
isfalse
, you seeBoolean
return that value.Here's a much simpler example:
There, I'm passing the function
testing
intofoo
. The argument name I'm using for that withinfoo
isbar
. The linebar();
calls the function.In case you're still having trouble with this, here's how I understand it (it gave me a headache too..)
A function is just a regular value. The previous sentence is key to understanding what's going on here.
Our noisy(f) function is a value. It is what it returns.
noisy(f) returns a function which takes an argument (arg).
noisy(f) also takes an argument (f). Inner functions (functions called from within functions) have access to variables and arguments which were passed to the outer function.
We're calling our outer function and passing it the argument Boolean. Our outer function returns its inner function which takes an argument (0). By understanding the above it should become clear that noisy(Boolean(0)) would simply pass an argument to our outer function, while not passing anything to the inner function which is returned by our outer function.
It's so simple really. Now that we understand it, it's hard to believe it gave us such a headache to begin with... */`
I'm relatively new to JS and I've also just been reading through Eloquent Javascript and I found it easier to understand once I understood the calling of the function (answering your point 1):
The
noisy(Boolean)
creates a new function and the(0)
is after it because it is being passed as an argument into that new function. If you refer back to the greater than example:It could also be called in this way:
I hope that clarifies your first question about why it was called that way.
For the second question. The
f
in:is the
Boolean
function that was passed intonoisy
whennoisy(Boolean)
was entered. It was then used as the argument in the noisy function. I also didn't realise that Boolean could be a function in itself and not just a data type. As others have said - it converts the argument you gave it into a Boolean value and returns the result.Therefore
val
becomesBoolean(arg)
which becomesBoolean(0)
which evaluates tofalse
. If you try callnoisy(Boolean)(1);
you will see it returntrue
. Theconsole.log("called with", arg, "- got", val);
simply logs the argument (0 in this case) and the result of evaluating it (false).In effect, it has changed the Boolean function into one that logs the argument and result, as well as returning the result.
I hope this helps. Just writing it has helped my own understanding.