回调函数在JavaScript示例中不起作用 [英] Callback Function is not working in JavaScript example

查看:130
本文介绍了回调函数在JavaScript示例中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下callback function example有什么问题?我传递了一些参数,最后,我传递了一个在其他任务完成时必须automatically运行的函数.那么,为什么我会出错?

What's wrong in the following callback function example? I am passing some parameters and in the end, I am passing a function that must automatically run when the other tasks are done. So, why am I getting an error?

期望:

我希望有2个console.logs.首先提供a, b, a+b的输出,然后进行第二次控制台打印hello.

I expected 2 console.logs. First giving output of a, b, a+b and second console printing hello.

示例:

function alpha(a, b, ()=>{
  console.log("hello");
}){
  console.log(a, b, a+b);
}
alpha(5, 10);

推荐答案

这是您想要的:

function alpha(a, b, fn) {
    console.log(a, b, a + b);
    fn();
}
alpha(5, 10, () => {
    console.log("hello");
});

// or defined by default
function alpha2(a, b, fn = () => {
    console.log("hello");
}) {
    console.log(a, b, a + b);
    fn();
}
alpha2(5, 10);

这篇关于回调函数在JavaScript示例中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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