什么是jQuery挂钩和回调? [英] What are jQuery hooks and callbacks?

查看:132
本文介绍了什么是jQuery挂钩和回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难概念化jQuery中的回调或挂钩.它们似乎混在一起,但我不知道它们之间的区别.

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.

据我从其他有关回调的文章(例如)了解到,回调只是一个函数传递给另一个函数B的A,一旦B完成,该函数将调用A. (这可能是完全错误的,如果是,请纠正我.)

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...

推荐答案

您的定义接近但不足.回调是一个函数A,您将其传递给另一个函数B(或对象B),B将在适当的时候调用 .

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.

许多函数或对象定义了一组可以触发回调的特定点"或事件.例如,如果您有一个控件通过AJAX定期进行自身更新,则它可能会定义一个将在更新时发生的事件,一个在更新返回后发生的事件以及一个在显示结果后发生的事件.对于这些事件中的任何一个,您都可以将自定义函数传递给它,该事件发生时应调用该函数.

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.

编辑后添加:

OP要求提供一个简单的钩子示例.

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

function getPeople( arrayOfUsernames, missingUsernameCallback )

在该函数中,每当我找到在数据中找不到的用户名时,我都会调用

Within the function, whenever I come to a username that I can't find in my data, I just call

missingUsernameCallback( notFoundUsername );

现在程序员可以像这样调用函数

Now the programmer can call the function like

getPeople( ["adam","betty","charlie"], function(name){ alert("Missing username " + name)} );

每当我遇到丢失的用户名时,我都会调用传递给我的函数.

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

   getPeople( ["adam","betty","charlie"], 
        {startOfListCallback:      function(){ alert("I'm starting the list")},
         missingUsernameCallback:  function(){ alert("Missing username"!)},
         endOfListCallback:        function(){ alert("I'm at the end of the list") });

等可能的回调列表将由该函数定义,并且应在API文档中(以及可能传递给回调的参数,等等).回调列表就是钩子.

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挂钩和回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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