为什么在JavaScript中使用回调,它的优点是什么? [英] Why use callback in JavaScript, what are its advantages?

查看:115
本文介绍了为什么在JavaScript中使用回调,它的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下,为什么我们在JavaScript中使用回调?我找到了示例,但它们可以通过使用普通函数来实现。使用它有什么好处?我得到了如何使用它的答案而不是为什么和何时我们需要使用它。

Could someone explain, why do we use callback in JavaScript? I found examples, but they could be implemented by using the normal functions. What is the advantage of using it? I got answers to "how" to use it and not "why and when" do we need to use it.

通常,我发现它在AJAX中使用。在 httpRequest.onreadystatechange 上。这类似于Java的多线程吗?响应的听众如何以及在哪里?
异步编程是否类似于多线程?

Typically, I found it being used in AJAX. on the httpRequest.onreadystatechange. Is this similar to Java's multi-threading? How and where is the listener for the response? Is asyncronous programming akin to multi-threading?

在下面的代码中,控制流程如何:

In the following code, how is the flow of control:

function some_function(arg1, arg2, callback) {
  var my_number = Math.ceil(Math.random() * (arg1 - arg2) + arg2);
  callback(my_number);
  some_different_function_not_callback(arg1);
}
some_function(5, 15, function(num) {
   console.log("callback called! " + num);
});

来自JQuery网站:

From the JQuery website:


关于回调的特殊之处在于,父之后出现的函数可以在回调执行之前执行(ref: http://docs.jquery.com/Tutorials:How_jQuery_Works

可能有人用一个例子向我解释这一行?

Could someone explain me this line with an example?

推荐答案

主浏览器进程是单线程事件循环。如果执行long在单线程事件循环中运行操作,进程阻塞。这很糟糕,因为进程在等待操作完成时停止处理其他事件。alert是为数不多的阻塞浏览器方法之一:如果你调用alert ('test'),您不能再单击链接,执行Ajax查询或与浏览器UI交互。

The main browser process is a single threaded event loop. If you execute a long-running operation within a single-threaded event loop, the process "blocks". This is bad because the process stops processing other events while waiting for your operation to complete. 'alert' is one of the few blocking browser methods: if you call alert('test'), you can no longer click links, perform ajax queries, or interact with the browser UI.

为了防止长期阻止阻止在XML操作中,XMLHttpRequest提供了一个异步接口。你在操作完成后传递一个回调来运行,当它正在处理时,它会控制回主事件循环而不是阻塞。

In order to prevent blocking on long-running operations, the XMLHttpRequest provides an asynchronous interface. You pass it a callback to run after the operation is complete, and while it is processing it cedes control back to the main event loop instead of blocking.

没有理由使用回调除非你想要将某些内容绑定到事件处理程序,否则你的操作可能会阻塞,因此需要一个异步编程接口。

There's no reason to use a callback unless you want to bind something to an event handler, or your operation is potentially blocking and therefore requires an asynchronous programming interface.

这是一个优秀的视频讨论了有关浏览器中使用的事件循环以及node.js中服务器端的更多信息。

编辑:那条复杂的线路来自jQuery文档只是意味着回调执行异步,因为控件被转回主事件循环。

that convoluted line from the jQuery documentation just means that the callback executes asynchronously as control is ceded back to the main event loop.

parent_function(function () { console.log('Callback'); });
parent_doesnt_block(); // <-- function appears after "parent"
therefore_execution_continues();
// Maybe we get 'Callback' in the console here? or maybe later...
execution_still_continues();

这篇关于为什么在JavaScript中使用回调,它的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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