什么是 jQuery 挂钩和回调?
我很难概念化 jQuery 中的回调或挂钩到底是什么。它们似乎混为一谈,但我不知道它们之间的区别。
根据我从其他有关回调的帖子中了解到的信息,例如 this,回调只是您可以使用的函数 A传递给另一个函数 B,一旦 B 完成,该函数就会调用 A。 (这可能是完全错误的,如果是的话请纠正我)。
除了“你应该使用钩子/回调”之外,我真的对钩子没有任何概念。有些事情让我怀疑他们有那么相似......
I am having a tough time conceptualizing what exactly callbacks or hooks are in jQuery. They seem to be lumped together, but I don't know the difference between them.
From what I understand from other posts about callbacks, like this, a callback is simply a function A that you pass to another function B that calls A once B is done. (That may be totally wrong, correct me if so).
I really don't have any notion on hooks, other than the statement "you should use a hook/callback." Something makes me doubt they're that similar...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的定义很接近但还不够。回调是一个函数 A,您将其传递给另一个函数 B(或对象 B),B 将在适当的点调用该函数。
对于某些函数,“适当的点”是在该函数完成之后,正如您所描述的。
许多函数或对象定义了一组可以触发回调的“某些点”或事件。例如,如果您有一个通过 AJAX 定期更新自身的控件,它可能会定义一个在即将更新时发生的事件、一个在更新返回后发生的事件以及一个在显示结果后发生的事件。对于这些事件中的任何一个,您都可以向其传递您的自定义函数,该函数在该事件发生时应调用。
可以调用自定义函数的定义点集就是人们所说的“钩子”。您可以在它们中将功能挂接到预构建的库中。
编辑添加:
OP 要求提供一个简单的钩子示例。
假设我编写了一个函数,它接受用户名列表,旨在遍历该列表,查找每个人的详细信息,并返回填充的人员对象列表。
也许我想让使用我的函数的程序员能够指定当在我的数据库中找不到用户名时应该发生什么。一个程序员可能希望函数出错,另一个程序员可能希望它忽略丢失的示例,另一个程序员可能希望它在不同的数据库中查找,另一个程序员可能希望它构造一个空的 person 对象。
所以我的函数签名可能类似于
在函数内,每当我遇到在数据中找不到的用户名时,我只需调用
现在程序员可以像这样调用该函数
,每当我遇到丢失的用户名时,我会调用传递给我的函数。
对于更复杂的情况,我可能需要接受一个包含多个回调函数的对象,因此可以像等
那样调用它。可能的回调列表将由函数定义,并且应该在 API 文档中(以及可以使用的参数)被传递到回调等)。该回调列表就是钩子。
Your definition is close but insufficient. A callback is a function A that you pass to another function B (or to an object B) that B will call at the appropriate point.
For some functions, "the appropriate point" is after that function completes, as you describe.
Many functions or objects define a set of "certain points" or events when callbacks can be fired. For example, if you have a control that updates itself periodically via AJAX, it might define an event that occurs when it's about to update, an event that occurs after the update returns, and an event that occurs after the results have been displayed. For any of these events, you could pass it your custom function that should be called when that event occurs.
That defined set of points where a custom function might be called is what people mean by "hooks". They're places that you can hook your functionality into a prebuilt library.
Edited to add:
The OP asked for a simple example of a hook.
Let's say I've written a function that takes a list of usernames and is designed to go through the list, look up each person's details, and return a populated list of people objects.
Maybe I want to give the programmer making use of my function the ability to specify what should happen when a username isn't found in my database. One programmer might want the function to error out, another might want it to just ignore the missing example, another might want it to look in a different database, another might want it to construct an empty person object.
So my function signature might be something like
Within the function, whenever I come to a username that I can't find in my data, I just call
Now the programmer can call the function like
and whenever I hit a missing username, I'll call the function that was passed to me.
For a more complex case, I might need to accept an object containing multiple callback functions, so it could be called like
etc. The list of possible callbacks will be defined by the function and should be in the API documentation (along with parameters that could be passed into the callbacks, etc.). That list of callbacks are the hooks.
我猜想在上下文中您已经看到这个词使用的意思是回调,这正是您所认为的(在 jQuery 中,钩子和回调是同一件事)。
例如,jQuery 的 animate() 函数的回调将在动画完成后立即执行。
一些内置函数和其他 jQuery 插件可能会为执行的各个阶段定义多个回调。例如,为用户显示弹出框的插件可能会在用户单击链接以显示该框时进行回调,但在实际显示该框之前(此回调可用于存储实际弹出框将显示的一些数据)例如需要)。当再次关闭框时,相同的弹出函数也可能执行回调函数 - 例如,此回调可用于对页面进行更改,或保存一些数据。
I would guess in the context you've seen the word used people mean callbacks, which are exactly what you think they are (in the case of jQuery a hook and a callback are the same thing).
A callback for jQuery's animate() function for instance will execute as soon as the animation has completed.
Some built in functions and other jQuery plugins might define multiple callbacks for various stages of of execution. For instance a plugin that displays a popup box for a user might have a callback for when the user clicks on a link to show the box, but before the box is actually displayed (this callback could be used to store some data the actual popup will need, for instance). The same popup functions might also execute a callback function when the box is closed again - this callback could be used to make a change to the page, or save some data for instance.