简体   繁体   中英

Please explain me this higher-order function javascript code

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM