jQuery新手:jQuery(function($){...})是什么意思? [英] jQuery newbie: what does jQuery(function($) { ... }) means?
问题描述
我知道在jQuery中,$(callback)
与jQuery(callback)
相同,与$(document).ready()
具有相同的效果.
I know in jQuery, $(callback)
is the same as jQuery(callback)
which has the same effect as $(document).ready()
.
怎么样
jQuery(function($) {
});
有人可以向我解释这种功能是什么意思吗?
Can some one explain to me what does this kind of function mean?
它是做什么的?
这和$(callback)
??
这个和$(function())
??有什么区别?
what is the difference between this one and $(function())
??
推荐答案
jQuery(function($) {
});
是这三个版本中最安全的版本.它使$
成为局部变量,从而优雅地避免了与可能使用$
符号的任何其他变量的冲突.
is the safest version of all three. It makes $
the local variable and thus gracefully avoids the conflicts with any other variables which possibly use $
symbol.
我认为它也是最近才添加的,不记得之前看到过.
I think it was also added fairly recently, don't remember seeing it before.
这些功能都做同样的事情-在DOM准备就绪时执行一些代码. $(document).ready(function(){})
是原始版本,它与基础javascript API匹配.
These function all do the same things - execute some code when DOM is ready. $(document).ready(function(){})
is the original one, and it matches the underlying javascript API.
"$"和"jQuery"被创建为快捷方式,以避免重复这种常见的构造.接受一个接受$作为其第一个参数的函数是另一个语法糖-现在您可以方便地进行闭包操作,而无需自己执行.
"$" and "jQuery" which accept function as an arguments were created as shortcuts to avoid repeating such a common construct. Accepting a function which accepts $ as its first argument is a further syntax sugar - now you get convenience of the closures without the need to do it yourself.
这篇关于jQuery新手:jQuery(function($){...})是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!