如何用简单的英语解释回调?它们与从另一个函数调用一个函数有何不同? [英] How to explain callbacks in plain english? How are they different from calling one function from another function?

查看:19
本文介绍了如何用简单的英语解释回调?它们与从另一个函数调用一个函数有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用通俗的英语解释回调?它们与从调用函数中获取一些上下文的另一个函数调用一个函数有什么不同?如何向新手程序员解释他们的力量?

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer?

推荐答案

我将尽量保持简单.回调"是由另一个函数调用的任何函数,该函数将第一个函数作为参数.很多时候,回调"是在某事发生时调用的函数.用程序员的话说,某事可以称为事件".

I am going to try to keep this dead simple. A "callback" is any function that is called by another function which takes the first function as a parameter. A lot of the time, a "callback" is a function that is called when something happens. That something can be called an "event" in programmer-speak.

想象一下这种情况:您期待几天后收到包裹.该包裹是送给邻居的礼物.因此,一旦您收到包裹,您就希望将其带给邻居.你出城了,所以你给你的配偶留下了指示.

Imagine this scenario: you are expecting a package in a couple of days. The package is a gift for your neighbor. Therefore, once you get the package, you want it brought over to the neighbors. You are out of town, and so you leave instructions for your spouse.

你可以告诉他们把包裹拿去给邻居.如果你的配偶像电脑一样愚蠢,他们会坐在门口等着包裹,直到它来(不做任何其他事情),一旦它来了,他们就会把它带给邻居.但是有更好的方法.告诉您的配偶,一旦他们收到包裹,就应该把它带给邻居.然后,他们可以正常生活,直到他们收到包裹.

You could tell them to get the package and bring it to the neighbors. If your spouse was as stupid as a computer, they would sit at the door and wait for the package until it came (NOT DOING ANYTHING ELSE) and then once it came they would bring it over to the neighbors. But there's a better way. Tell your spouse that ONCE they receive the package, they should bring it over the neighbors. Then, they can go about life normally UNTIL they receive the package.

在我们的例子中,包裹的接收是事件",把它带给邻居是回调".您的配偶执行"您的指示,只在包裹到达时 将包裹带过来.好多了!

In our example, the receiving of the package is the "event" and the bringing it to the neighbors is the "callback". Your spouse "runs" your instructions to bring the package over only when the package arrives. Much better!

这种思维在日常生活中是显而易见的,但计算机没有同样的常识.考虑程序员通常如何写入文件:

This kind of thinking is obvious in daily life, but computers don't have the same kind of common sense. Consider how programmers normally write to a file:

fileObject = open(file)
# now that we have WAITED for the file to open, we can write to it
fileObject.write("We are writing to the file.")
# now we can continue doing the other, totally unrelated things our program does

在这里,我们在写入文件之前等待文件打开.这阻塞"了执行流程,我们的程序无法做它可能需要做的任何其他事情!如果我们可以这样做呢:

Here, we WAIT for the file to open, before we write to it. This "blocks" the flow of execution, and our program cannot do any of the other things it might need to do! What if we could do this instead:

# we pass writeToFile (A CALLBACK FUNCTION!) to the open function
fileObject = open(file, writeToFile)
# execution continues flowing -- we don't wait for the file to be opened
# ONCE the file is opened we write to it, but while we wait WE CAN DO OTHER THINGS!

事实证明,我们使用某些语言和框架来做到这一点.它太酷了!查看 Node.js 以实际练习这种思维.

It turns out we do this with some languages and frameworks. It's pretty cool! Check out Node.js to get some real practice with this kind of thinking.

这篇关于如何用简单的英语解释回调?它们与从另一个函数调用一个函数有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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