简体   繁体   English

请向我解释此高阶函数javascript代码

[英]Please explain me this higher-order function javascript code

I'm studying higher order functions following the Eloquent JavaScript book. 我正在研究Eloquent JavaScript本书之后的高阶函数。 I haven't been able to understand this code, why is "Boolean" passed as noisy first argument? 我无法理解此代码,为什么将“ Boolean”作为嘈杂的第一个参数传递?

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. noisy接受任何一个参数的函数作为其参数。 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. Boolean只是他们使用的示例函数。 It converts its argument to a boolean datatype. 它将其参数转换为布尔数据类型。

Boolean is a constructor function for the Boolean type. 布尔值是布尔值类型的构造函数。 It could be any function. 它可以是任何功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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