回调函数在JavaScript示例中不起作用

编程入门 行业动态 更新时间:2024-10-10 10:28:44
本文介绍了回调函数在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示例中不起作用

本文发布于:2023-11-27 21:19:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   回调   中不   函数   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!