Please explain me this higher-order function javascript code

瘦欲@ 提交于 2019-12-02 16:06:18

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/29550086/please-explain-me-this-higher-order-function-javascript-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!