$.noop() 在 jQuery 1.4 中的真正用途是什么?

发布于 2024-08-17 22:13:38 字数 316 浏览 13 评论 0原文

在翻阅有关 jQuery 1.4 的发行说明时,我发现了 $.noop() 这是:

描述:一个空函数。 (1.4中添加)

当您希望传递一个不执行任何操作的函数时,可以使用此空函数。

也许我在这里遗漏了一些深刻的东西,但是传递的实际用途到底是什么一个空函数?

代码示例表示赞赏。

Pouring over the release notes regarding jQuery 1.4, I came acrosss $.noop() which is:

Description: An empty function. (added in 1.4)

You can use this empty function when you wish to pass around a function that will do nothing.

Perhaps I'm missing something profound here, but what exactly is a practical use of passing around an empty function?

Code examples appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

郁金香雨 2024-08-24 22:13:39

如果一个函数需要你传递一个函数作为参数也许? do_something($.noop)do_something(function(){}) 更短。

虽然不多……

6个字符……

是的,这个功能实际上看起来没什么用。

If a function requires you pass a function as an argument maybe? It's shorter to say do_something($.noop) than do_something(function(){}).

Although not by much...

...6 characters...

...yeah, that feature looks quite useless actually.

梦屿孤独相伴 2024-08-24 22:13:39

如果您有一个函数可以向其他函数提供函数,那么它会很有用。

示例:您有一个数据列表。每个项目都有一个执行某些操作的按钮。每个项目的“某事”可能不同。您有一个“FunctionFactory”,它接受该项目并返回一个函数。如果您出于某种原因不希望按钮执行某些操作,那么最干净的方法可能是返回一个空函数,因为这样您就知道您的工厂始终返回一个函数。

我没有 jQuery 的具体示例,但我想这在 .each 或 .map 块中使用时会派上用场。

It can be useful if you have a function that supplies functions to other functions.

Example: You have a List of data. Each item has a Button that does something. The "something" can be different for every item. You have a "FunctionFactory" that takes in the item and returns a function. If you don't want the button to do something for whatever reason, then the cleanest way could be to return an empty function, as that way you know that your Factory ALWAYS returns a function.

I don't have a concrete example for jQuery, but I guess this could come in handy when used in an .each or .map block.

挖鼻大婶 2024-08-24 22:13:39

它纯粹是在需要回调的情况下对 function(){} 的一种便利/替代 - 我认为我不会很快使用它。

我敢打赌,当 jQuery 团队把它放进去时,他们一定会笑得很开心,这也有一个喜剧目的。

It's purely a convenience/replacement for function(){} in the context of where callbacks are required - I don't think I'll be using it anytime soon.

I bet the jQuery team had quite a laugh when they dropped it in though, also serves a comedic purpose.

给我一枪 2024-08-24 22:13:38

之所以提出此函数,是因为在嵌入式系统上使用 $.ajax 时会出现性能问题,据 jQuery-Dev 邮件列表报道。您可以查看帖子

基本上,他们更喜欢引入和使用这个单一的空函数,而不是到处声明空的匿名函数。

现在这个函数在 ajax 内部使用,事件偏移 模块。

您还可以查看引入时的提交

This function was proposed due to performance issues on embedded systems when using $.ajax, reported on the jQuery-Dev mailing list. You can see the thread.

Basically, they preferred to introduce and use this single empty function, rather than declaring empty anonymous functions all around.

Now this function is internally used in the ajax, event and offset modules.

You can give a look to the commit when it was introduced also.

人生百味 2024-08-24 22:13:38

如果您有一个接受函数作为参数的函数,并且没有任何代码来提供它,则可以传递 $.noop

不过,我想不出 jQuery 中参数不是可选的任何此类情况。

与编写 function(){} 不同,传递 $.noop 不会创建新的函数实例,从而节省了一点内存。但是,如果您传递给它的任何内容修改了函数对象(例如,funcParam.id = 2),则传递$.noop 将会把事情搞砸。

If you have a function that accepts a function as a parameter, and you don't have any code to give it, you can pass $.noop.

I can't think of any such cases in jQuery where the parameter isn't optional in the first place, though.

Unlike writing function(){}, passing $.noop will not create a new function instance, saving a bit of memory. However, if whatever you're passing it to modifies the function object (eg, funcParam.id = 2), passing $.noop will mess things up.

不一样的天空 2024-08-24 22:13:38

真实世界示例(几乎):

jQuery.fn.myAwesomeAjax = function(url, complete) {
  return jQuery.ajax(url || this.url)
    .complete(complete || jQuery.noop);
};

使用它代替 function (){}

Real World Example (well almost):

jQuery.fn.myAwesomeAjax = function(url, complete) {
  return jQuery.ajax(url || this.url)
    .complete(complete || jQuery.noop);
};

Use it instead of function (){}

近箐 2024-08-24 22:13:38

也许如果某些不好的 API 需要一个函数作为参数,而您又不想在其中执行任何操作,那么这将是一种框架支持的方式,可以使这一点变得显而易见。

Probably if some bad API requires a function as a parameter, and you don't want to do anything in it, this would be a framework-supported way of making that obvious.

2024-08-24 22:13:38

我使用了几个需要回调的插件,但对于某些部分我实际上不想使用特定的回调。所以,我放入了 function() {}。

noop 在 jQuery 源代码中定义,

noop: function() {}

因此它适合您使用空白函数的任何地方,例如上面的示例。

I use a couple of plugins which require callbacks, but for some parts I don't actually want to use a certain callback. So, I put in function() {}.

noop is defined in the jQuery source as

noop: function() {}

so it will fit anywhere you'd use a blank function, such as the above example.

那伤。 2024-08-24 22:13:38

唯一合乎逻辑的原因是,如果您正在调用一个执行某些操作的函数并调用另一个函数,并且您希望更高级别的函数在不调用参数函数的情况下执行其操作。

大多数 jQuery 函数都可以选择采用参数函数,因此您不必传入一个参数函数。也许有一两个情况并非如此,或者可能是为了帮助开发人员使用其行为类似的自定义代码。

The only logical reason is if you're calling a function that does something AND calls another function, and you want the higher-level function to do its thing without calling a parameter function.

Most of the jQuery functions optionally take a parameter function, so you don't have to pass one in. Maybe there's one or two where that's not the case -- or maybe it's to assist developers with their custom code that behaves like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文