传递参数到回调 [英] passing arguments to callback

查看:233
本文介绍了传递参数到回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个函数可以获取一个回调函数,像这样

  function doSomething({callbackSuccess:myFunction,callbackError:myOtherFunction})

$



如果myOtherFunction得到一个msg参数,如下


$ b

我如何传递参数到myFunction或myOtherFunction?

  function myOtherFunction(msg){
alert(msg);先感谢提前

h2_lin>解决方案

最简单

  function dosomething回调){
// some code-poetry;
callback(1,2,3); //回调是回来的!
}

function foo(a,b,c){//这将是我的回调
alert(我有一些参数)
}

dosomething(foo); //函数调用

回调函数的最佳解释:检查此



一个简单的定义:当某些代码或某个函数完成执行时,在某个时刻调用该回调函数。


How can I pass arguments to a callback function in this specific scenario.

I have a function that gets an object with callbacks like this

function doSomething({ callbackSuccess : myFunction, callbackError : myOtherFunction})

how can I pass arguments to myFunction or to myOtherFunction?

Like, if myOtherFunction gets a msg parameter like this

function myOtherFunction(msg) {
   alert(msg);
}

Thanks in advance

解决方案

Simplest of all:

function dosomething(callback) {
    //some code-poetry;
    callback(1, 2, 3); //the callback is back!!!
}

function foo(a, b, c) {//this will be my callback
    alert("I got some parameters")
}

dosomething(foo); //the function call

Best explanation for a callback function:Check this

A simple defination would be:the callback function is called at a certain point in the future when some code or a function completes execution.

这篇关于传递参数到回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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