Javascript中的高阶函数 [英] Higher-order functions in Javascript

查看:88
本文介绍了Javascript中的高阶函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Eloquent JavaScript(新版),并且我到达了部分是高阶函数,我对以下代码中发生的事情感到困惑。

 函数噪声(f){
返回函数(arg){
console.log(calling与,arg);
var val = f(arg);
console.log(with called,arg, - got,val);
返回val;
};
}
noisy(布尔)(0);
//→用0
调用//→用0调用 - 得到false




  1. 为什么这样的函数调用噪声很大? (布尔)是否是cast?一个演员什么?返回值?还是争论?为什么不(布尔)噪声(0)如果它的返回值。或者嘈杂((布尔)0)如果参数是被转换的参数。

      noisy(布尔)(0)


  2. 这一行发生了什么? f()甚至定义在哪里?

      var val = f(arg); 



解决方案


  1. 布尔值是一个函数。这是你通过 noisy 间接调用的函数。我知道有点混乱,因为它看起来像一个类型的名字。但是在JavaScript中,那些最初被封顶的东西( Boolean Number String 等等)是功能。当您调用布尔 without 使用 new )时,它会尝试将您的参数给它一个布尔值原始值并返回结果。 (请参阅规范中的第15.6.1节。 )


  2. f 是 noisy code> function。


JavaScript中的函数是第一类对象。您可以像其他任何对象一样将它们作为参数传递给其他函数。



当您执行

  noisy(布尔)(0)

有两件事情正在进行。首先:

  //(实际上,我们并没有真正创建变量......)
var x = noisy(布尔);

这给我们一个函数,当被调用时,它会调用 Boolean 与我们给出的参数同时做这些 console.log 语句。这是你在 noisy return function(arg)... )中创建的函数;



然后我们调用这个函数:

  x(0); 

这就是当您看到控制台输出时。由于布尔(0) false ,您可以看到布尔值返回该值。



这是一个更简单的例子:

 函数foo(bar){
bar();

函数测试(){
alert(测试被调用);
}
foo(测试);

在那里,我传递函数测试转换为 foo 。我在 foo 中使用的参数名称是 bar 。行 bar(); 调用函数。


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

  1. 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)
    

  2. What is happening in this line? Where is f() even defined?

    var val = f(arg);
    

解决方案

  1. Boolean is a function. It's the function you're calling indirectly through noisy. 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 call Boolean (without using new), it tries to convert the argument you gave it into a boolean primitive value and returns the result. (See §15.6.1 in the spec.)

  2. f is the name of the argument in the noisy 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

noisy(Boolean)(0)

There are two things going on. First:

// (In effect, we're not really creating a variable...)
var x = noisy(Boolean);

That gives us a function that, when called, will call Boolean with the argument we give it while also doing those console.log statements. This is the function you see being created in noisy (return function(arg)...);

Then we call that function:

x(0);

And that's when you see the console output. Since Boolean(0) is false, you see Boolean return that value.

Here's a much simpler example:

function foo(bar) {
    bar();
}
function testing() {
    alert("testing got called");
}
foo(testing);

There, I'm passing the function testing into foo. The argument name I'm using for that within foo is bar. The line bar(); calls the function.

这篇关于Javascript中的高阶函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆