JavaScript / jQuery闭包函数语法 [英] JavaScript / jQuery closure function syntax
问题描述
有人可以解释以下功能之间的差异:
Can someone please explain the differences between the following functions:
(function($){
// can do something like
$.fn.function_name = function(x){};
})(jQuery);
我可以在下一个函数中使用jQuery吗?
Could I use jQuery in the next function?
(function(){
}());
以下与jquery.ready()相同?
And is the following the same as jquery.ready()?
$(function(){
});
谢谢!
推荐答案
(function($){
// can do something like
$.fn.function_name = function(x){};
})(jQuery);
这是一个使用 $ $ c $的自执行匿名函数c>在参数中,以便您可以在该函数内使用它(
$
)而不是 jQuery
而不用担心与其他库冲突,因为在其他库中, $
具有特殊含义。在编写jQuery插件时,该模式特别有用。
That is self-executing anonymous function that uses $
in argument so that you can use it ($
) instead of jQuery
inside that function and without the fear of conflicting with other libraries because in other libraries too $
has special meaning. That pattern is especially useful when writing jQuery plugins.
您可以在那里写任何字符而不是 $
:
You can write any character there instead of $
too:
(function(j){
// can do something like
j.fn.function_name = function(x){};
})(jQuery);
这里 j
会自动赶上指定的jQuery最后(jQuery)
。或者你可以完全忽略参数但是你必须使用 jQuery
关键字而不是 $
而不是害怕碰撞仍然。所以 $
包含在短手的参数中,这样你就可以写 $
而不是 jQuery
在该函数内部。
Here j
will automatically catch up jQuery specified at the end (jQuery)
. Or you can leave out the argument completely but then you will have to use jQuery
keyword all around instead of $
with no fear of collision still. So $
is wrapped in the argument for short-hand so that you can write $
instead of jQuery
all around inside that function.
(function(){
}());
这是自动执行的匿名函数,但由于<$而没有参数和运行/调用本身c $ c>()最后。
That is self-executing anonymous function again but with no arguments and runs/calls itself because of ()
at the end.
在某些情况下,这种模式非常有用。例如,假设你想在每次500毫秒后运行一段代码,你自然会选择 setInterval
。
That patterns turns out to be extremely useful in some situations. For example, let's say you want to run a piece of code after 500 milli seconds each time, you would naturally go for setInterval
.
setInterval(doStuff, 500);
但是如果 doStuff
函数需要多于500毫秒做它正在做的事情?你会看到意想不到的结果,但是 setInterval
将继续在指定的时间再次调用该函数,而不管 doStuff
是否已完成。
But what if doStuff
function takes more than 500 mill-seconds to do what it is doing? You would witness unexpected results but setInterval
will keep on calling that function again and again at specified time irrespective of whether doStuff
finished.
这就是那种模式的来源,你可以用 setTimeout
与自我组合做同样的事情。执行匿名功能并避免错误 setInterval
,如下所示:
That is where that pattern comes in and you can do the same thing with setTimeout
in combination with self-executing anonymous function and avoid bad setInterval
like this:
(function foo(){
doStuff;
setTimeout(foo, 500);
})()
此代码也会一次又一次地重复,但有一点不同。除非 doStuff
完成,否则永远不会触发 setTimeout
。比使用不好的 setInterval
更好的方法。
This code will also repeat itself again and again with one difference. setTimeout
will never get triggered unless doStuff
is finished. A much better approach than using bad setInterval
.
注意你也可以编写这样的自执行匿名函数:
Note that you can also write self-executing anonymous function like this:
function(){
// some code
}();
使用额外的括号(如之前的功能
关键字)只是编码约定,可以在Crackford的着作,jQuery和其他地方看到。
Using extra braces around (like before function
keyword) is simply coding convention and can be seen in Crackford's writings, jQuery and elsewhere.
$(function(){
});
这是就绪处理程序的简写语法:
That is short-hand syntax for ready handler:
$(document).ready(function(){
});
更多信息:
- How Self-Executing Anonymous Functions Work
这篇关于JavaScript / jQuery闭包函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!