0

I'm studying higher order functions following the Eloquent JavaScript book. I haven't been able to understand this code, why is "Boolean" passed as noisy first argument?

This is supposed to be function that changes other function, I just don't get how it works!

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

2 Answers 2

4

noisy accepts any one-argument function as its argument. It returns a new function that calls that function, but displays messages before and after it calls it.

Boolean is just an example function that they used. It converts its argument to a boolean datatype.

Sign up to request clarification or add additional context in comments.

Comments

0

Boolean is a constructor function for the Boolean type. It could be any function.

2 Comments

It's not using new, it's just calling the Boolean function.
Sorry, I wrote this hastily. Take Barmar's answer instead. As far as I can see, the noisy() function is pretty useless. What I really wanted to point out is that Boolean is a constructor function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.