如何在JavaScript中传递和调用函数作为参数? [英] How to pass and call a function as an argument in Javascript?

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

问题描述

我正在寻找将匿名函数传递给另一个函数,但它似乎没有按照我的意愿工作。

I'm looking to pass an anonymous function to another function, but it doesn't seem to be working as I'd like.

我已附加代码,我认为它会给你一个更好的想法,我会做什么。

I've attached the code, I think it'd give you a better idea what I'd to do.

如何成功地将函数作为参数传递,然后调用它?

How do I successfully pass a function as an argument, and then invoke it?

<script language="javascript" type="text/javascript">
function do_work(success) {
    success;
}

do_work(function () {
    alert("hello")
});

</script>


推荐答案

您必须实际调用函数:

function do_work(success) {
    success();
}

这篇关于如何在JavaScript中传递和调用函数作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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