【问题标题】:What's the use of callback function in the case given below?在下面给出的情况下,回调函数有什么用?
【发布时间】:2020-09-29 10:00:00
【问题描述】:

我写了两个函数。第一个使用回调函数,第二个不使用。但是这两个函数都打印同样的东西。那么这里有什么区别呢?这种情况下不需要回调函数吗?

//function 1

function printHi(name, callback){
    console.log('Hi '+name);
    callback(name);
}
function printBye(name){
    console.log('Bye '+name)
}

printHi('lavanya',printBye);.

/*************************************************************************/
//function 2

function printHi(name){
    console.log('Hi '+name);
    printBye(name);
}
function printBye(name){
    console.log('Bye '+name)
}

printHi('lavanya');

【问题讨论】:

    标签: javascript function ecmascript-6 callback ecmascript-5


    【解决方案1】:

    第一个版本中的printHi 是一个高阶函数,而第二个版本不是。

    第一个版本中的printHi 在参数方面是独立的,它不访问全局变量,但在第二个版本中,它从全局范围printBye 访问一个函数。

    你可以阅读更多关于高阶函数here

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2021-12-05
      • 2010-11-01
      • 1970-01-01
      相关资源
      最近更新 更多