$.noop() 在 jQuery 1.4 中的真正用途是什么?
在翻阅有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果一个函数需要你传递一个函数作为参数也许?
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)
thando_something(function(){})
.Although not by much...
...6 characters...
...yeah, that feature looks quite useless actually.
如果您有一个函数可以向其他函数提供函数,那么它会很有用。
示例:您有一个数据列表。每个项目都有一个执行某些操作的按钮。每个项目的“某事”可能不同。您有一个“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.
它纯粹是在需要回调的情况下对
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.
之所以提出此函数,是因为在嵌入式系统上使用
$.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.
如果您有一个接受函数作为参数的函数,并且没有任何代码来提供它,则可以传递
$.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.真实世界示例(几乎):
使用它代替
function (){}
Real World Example (well almost):
Use it instead of
function (){}
也许如果某些不好的 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.
我使用了几个需要回调的插件,但对于某些部分我实际上不想使用特定的回调。所以,我放入了 function() {}。
noop 在 jQuery 源代码中定义,
因此它适合您使用空白函数的任何地方,例如上面的示例。
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
so it will fit anywhere you'd use a blank function, such as the above example.
唯一合乎逻辑的原因是,如果您正在调用一个执行某些操作的函数并调用另一个函数,并且您希望更高级别的函数在不调用参数函数的情况下执行其操作。
大多数 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.