简体   繁体   English

“回调”只是命名约定吗?

[英]are 'callbacks' just a naming convention?

i've read that callbacks are functions triggered after the rest of the logic is done like this: 我读过回调是在其余逻辑完成后触发的函数,如下所示:

function withCallback(argument, callback) {
  doSomethingWithParams(argument);
  callback():
}

but as far as i can tell these callbacks need to be declared all the same, there is no universal method of putting callbacks wherever you want every time? 但是据我所知,这些回调都需要声明为完全相同,没有一种通用的方法可以每次都将回调放置在任何地方? i guess my question is: are callbacks special in a technical way, or is it just a conventional name for a function declared and executed at the end of another function? 我想我的问题是:回调在技术上是特殊的,还是只是在另一个函数的末尾声明和执行的函数的常规名称?

There's nothing special about callbacks; 回调没有什么特别的。 they're just functions (in JavaScript). 它们只是函数(在JavaScript中)。 The term, "callback," is used to indicate that the function provided by the caller is called back at some appropriate time as defined by whatever is accepting the callback. 术语“回调”,用于指示由任何正在接受回调所限定由呼叫者提供的功能在一些适当的时间称为背面 Even then, we don't always use the term "callback." 即使那样,我们也不总是使用术语“回调”。 Event handlers are callbacks, for instance, but we hardly ever call them that. 例如,事件处理程序是回调,但我们几乎从未将其称为回调。

...as far as i can tell these callbacks need to be declared all the same... ...据我所知,这些回调必须全部声明为...

Not at all. 一点也不。 The onreadystatechange callback for XMLHttpRequest doesn't receive any arguments, but the Array#sort callback receives two, while Array#forEach receives three (though often people only use one or two of them). XMLHttpRequestonreadystatechange回调不接收任何参数,但是Array#sort回调接收两个,而Array#forEach接收三个(尽管人们通常只使用其中一个或两个)。 They're all different. 他们都是不同的。 Nor do all callbacks have to be defined inline, and often, they aren't. 并非所有回调都必须内联定义,而且通常不是。 So they aren't all declared the same way. 因此,它们的声明方式并不完全相同。

There are three common "standard" forms of callbacks that I know of, though: 不过,我知道三种常见的“标准”回调形式:

  1. DOM event handlers: When called, they receive an event object as their first argument, and in the normal case, this will be set to the element on which the handler was registered. DOM事件处理程序:当被调用时,它们接收事件对象作为它们的第一个参数,并且在正常情况下, this将被设置为在其上的处理程序被注册的元素。 Its return value is ignored in the standard form ( addEventListener ) but used for some things by others (see my post: The true story of return false ). 它的返回值以标准格式( addEventListener )被忽略,但被其他人用于某些事情(请参阅我的文章: return false的真实故事 )。

  2. Promise callbacks, which come in two forms: 承诺回调,有两种形式:

    1. The promise executor , which is the function you pass new Promise , which receives two arguments: The function to use to resolve the project, and the function to use to reject it. new Promise executor是您传递给new Promise的函数,该函数接收两个参数:用于解析项目的函数和用于拒绝项目的函数。

    2. The then and catch callback, which receives (in a standard implementation) a single argument which is the resolution value or error that occurred. thencatch回调,该回调(在标准实现中)接收单个参数,该参数是发生的分辨率值或错误。 The return value of the callback settles the promise created by the call to then or catch , and throwing an error from either of them rejects that promise. 回调的返回值将解决对thencatch的调用所创建的承诺,并且抛出错误都会拒绝该承诺。

  3. Node.js-style API callbacks: These receive an error or null as their first argument, followed by zero or more non-error values if null is passed as the first argument. Node.js风格的API回调:这些回调将第一个参数接收到错误或null值,如果将null用作第一个参数,则返回零个或多个非错误值。

There are probably others. 可能还有其他人。

Callbacks are just functions and they're named very appropriately. 回调只是函数,它们的命名非常恰当。 They are "called back" when an operation is complete, when a resource is ready etc. Callbacks are not unique to JavaScript, they've been around for decades. 当操作完成,资源准备就绪等时,它们被“回调”。回调并不是JavaScript独有的,它们已经存在了数十年。 Anytime a caller wants to make an asynchronous call and receive the result at a later date, this pattern makes sense. 每当调用者想要进行异步调用并在以后接收结果时,此模式都是有意义的。

With regard to standardization, there is no standard way of defining how a callback should look, however you'll find that they are similar, for example, Node.js uses an error first callback signature, passing any error as the first argument to the callback function. 关于标准化,没有定义回调外观的标准方法,但是您会发现它们相似,例如,Node.js使用错误优先的回调签名,将任何错误作为第一个参数传递给回调函数。

为了使其更清晰易读,可以使用name next代替callback

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

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