本文介绍了回调函数在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示例中不起作用
发布评论