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())
有什么区别??
推荐答案
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屋!