【问题标题】:Is a callback also known as a higher-order function?回调是否也称为高阶函数?
【发布时间】:2019-06-15 06:42:19
【问题描述】:

我正在尝试理解 callbackhigher-order 函数,但是博客文章中的描述 Understand JavaScript Callback Functions and Use Them 让我感到困惑,其含义是它们是一回事:

回调函数,也称为高阶函数,...

在 Quora 对有关What is a simple explanation of higher order functions and callbacks in JavaScript? 的问题的回答中逐字重复。

这对我来说没有意义。据我了解,高阶函数接收或返回其他函数回调函数是被传递的函数/接受了,那怎么可能同时兼具呢?该描述有什么我不理解的地方吗?

【问题讨论】:

  • 我会使用维基百科的定义——那篇文章似乎使用了错误的术语。
  • 很多人会混淆这些概念,因为它们之间的关系非常密切。
  • 如果您对这个概念有疑问,请查看闭包。这将有助于更容易地理解差异。这可能有助于这项工作:medium.com/@zfrisch/…
  • @zfrisch 不,闭包的工作方式与定义术语“回调”和“高阶函数”无关。
  • @Bergi 我没有意识到其他人会在 cmets 中大声疾呼。但现在看来我什至无法访问该文章...

标签: javascript callback terminology higher-order-functions


【解决方案1】:

Callback function

回调函数是传递给另一个函数的函数 参数,然后在外部函数内部调用以完成 某种例行程序或行动。

Return a function

返回称为高阶函数的函数的函数

回调函数不是高阶函数,除非它是返回函数的函数。

简单回调:

function toto(callback){
  /** some routine or action before */
  callback();
}

function foo(){
  console.log("I'm a simple callback");
}

toto(foo);

简单的高阶函数

function toto(){
  console.log("I'm a simple Higher-Order Function")
  return function(){
     console.log("I'm the return function");
  }
}

//first log
const func = toto();
//second log
func();

也是高阶函数的回调:

function toto(callback){
  /** some routine or action before */
  const func = callback();
  func();
}

function foo(){
  console.log("I'm a callback and Higher-Order function");
  
  return function(){
    console.log("Do something...");
  };
}

toto(foo);

【讨论】:

  • 谢谢你的例子!绝对澄清了解释回调如何可能是高阶函数的浑水,但它们不一定是一回事。
【解决方案2】:

不,回调不一定是高阶函数。他们可以。您可以有一个接受另一个函数作为参数的回调。

回调是给予高阶函数的,这可能是导致混乱的原因。接受另一个函数作为参数的函数是导致它被归类为高阶的标准之一。

【讨论】:

    【解决方案3】:

    在我看来,高阶函数是一个接受另一个函数并使用它来抽象某些行为的函数,例如这个 c# 扩展方法:

        public static IEnumerable<T> OrderByProperty<T>(this IEnumerable<T> items, Func<T, object> selector)
        {
            return items.Select(x => new { o = selector(x), item = x })
                        .OrderBy(x => x.o)
                        .Select(x=> x.item);
        }
    

    它需要一个函数来确定,在对集合进行排序时会考虑哪个属性。 示例用法:

        var orderedByA = Enumerable.Range(0, 100)
              .Select(x=> new Item{
                A = x,
                B = 100 - x
              })
              .OrderByProperty(x => x.A);
    

    另一方面,当需要一些异步或长时间操作时,回调可用于继续应用程序流程,例如

    void FirstAsync(){
        Task.Run(()=>{
            Thread.Sleep(1000);
            Console.WriteLine("First executed");
        });
    }
    
    void Second()
    {
        Console.WriteLine("Second executed");
    }
    
    void FirstV2(Action callback)
    {
        Task.Run(() =>
        {
            Thread.Sleep(1000);
            Console.WriteLine("First executed");
            callback?.Invoke();
        });
    }
    
    void Main()
    {
        FirstAsync();
        Second();
        Thread.Sleep(2000);
        FirstV2(Second);
    }
    

    上面程序的输出会是这样的:

    Second executed
    First executed
    First executed
    Second executed
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多