简体   繁体   English

是否需要将javascript回调函数作为参数传递?

[英]Is passing a javascript callback function as a parameter required?

In everything I've read thus far, callback functions are passed as arguments into other functions: 到目前为止,在我读过的所有文章中,回调函数都作为参数传递给其他函数:

function mycallback(){
    //dosomething
}

function mainfunc(mycallback){
    //do something else
    mycallback();
}

mainfunc(mycallback);

Which works as you would expect, great. 正如您所期望的那样,效果很好。 My question is if the passing of the callback function as an argument into the mainfunc is requried? 我的问题是是否要求将回调函数作为参数传递给mainfunc? It seems if you omit this: 看来如果您忽略了这一点:

function mycallback(){
    //dosomething
}

function mainfunc(){
    //do something else
    mycallback();
}

mainfunc();

it works fine, and is identical to the first example. 它工作正常,并且与第一个示例相同。 But I don't see people using callbacks in this way. 但是我看不到人们以这种方式使用回调。 Is there a reason? 有什么原因吗? What am I missing? 我想念什么?

Callbacks are best used when you have asynchronous tasks. 当您执行异步任务时,最好使用回调。

Let's say mainfunc is an async operation. 假设mainfunc是异步操作。

  • In your first example, you handed a specific callback to mainfunc . 在第一个示例中,您将特定的回调传递给mainfunc This gives you flexibility to the caller to assign what function to execute after the operation. 这使调用者可以灵活地分配在操作后要执行的功能。

  • The second example, you hardcoded the callback. 第二个示例, 对回调进行了硬编码 Unless you have a reason to do so, like a predetermined post-processing function, this is not flexible. 除非您有理由这样做(如预定的后处理功能),否则这是不灵活的。

Technically, yes, you can use your functions like that. 从技术上讲,是的,您可以像这样使用您的函数。 The reason for passing a function as a parameter, is the same reason you would have for passing anything as a parameter. 传递函数作为参数的原因与传递任何东西作为参数的原因相同。

mainfunc(eatSevenBurritos);
mainfunc(eatTwoChimichangas);

Under certain conditions, maybe you'd rather eat 7 burritos while going through mainfunc. 在某些情况下,通过mainfunc时,您可能会想吃7个墨西哥卷饼。 Maybe under some other condition, you'd rather eat 2 chimichangas while going through mainfunc. 也许在其他条件下,您宁愿在通过mainfunc时吃2个chimichangas。

If you're always going to be running the same function, then go ahead and code it in there if you can. 如果您总是要运行相同的功能,那么请继续并在其中进行编码。 There's no point adding pointless complexity. 增加毫无意义的复杂性毫无意义。 But if you need that behavior to change, you need to treat it as a parameter. 但是,如果您需要更改该行为,则需要将其视为参数。

When folks talk about "first class functions" this sort of behavior is what they mean. 当人们谈论“一流的职能”时,这种行为就是他们的意思。 Being able to treat functions as a parameter. 能够将函数视为参数。 Because behavior is a type of data. 因为行为是一种数据类型。

You second approach works fine if you want a single function to handle all callbacks. 如果希望单个函数处理所有回调,则第二种方法可以很好地工作。

Often when you call a method that uses a callback, you want it to different things in the callback depending on when you call it and from where. 通常,当您调用使用回调的方法时,您希望它在回调中的不同之处取决于调用的时间和位置。 Instead of handling all different situations in a single callback function (which then has to be aware of which situation it is), you can simply use one callback function for each specific situation. 不必在单个回调函数中处理所有不同的情况(然后必须知道它是哪种情况),您可以针对每种特定情况简单地使用一个回调函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM