$(document).ready 简写
以下是 $(document).ready
的简写吗?
(function($){
//some code
})(jQuery);
我发现这种模式被广泛使用,但我找不到任何参考。如果它是 $(document).ready()
的简写,是否有任何特殊原因可能不起作用?在我的测试中,它似乎总是在就绪事件之前触发。
Is the following shorthand for $(document).ready
?
(function($){
//some code
})(jQuery);
I see this pattern used a lot, but I'm unable to find any reference to it. If it is shorthand for $(document).ready()
, is there any particular reason it might not work? In my tests it seems to always fire before the ready event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
简写是:
The shorthand is:
$(document).ready(handler)
的简写形式是$(handler)
(其中handler
是一个函数)。请参阅此处。您问题中的代码与
.ready()
无关。相反,它是一个以 jQuery 对象作为参数的立即调用函数表达式 (IIFE)。其目的是将至少$
变量的范围限制在其自己的块中,这样就不会引起冲突。您通常会看到 jQuery 插件使用的模式来确保$ == jQuery
。The shorthand for
$(document).ready(handler)
is$(handler)
(wherehandler
is a function). See here.The code in your question has nothing to do with
.ready()
. Rather, it is an immediately-invoked function expression (IIFE) with the jQuery object as its argument. Its purpose is to restrict the scope of at least the$
variable to its own block so it doesn't cause conflicts. You typically see the pattern used by jQuery plugins to ensure that$ == jQuery
.正确的简写是这样的:
您发布的代码...
...创建一个匿名函数并立即执行它,并将
jQuery
作为参数$
传入。它有效做的就是获取函数内的代码并像平常一样执行它,因为$
已经是jQuery
的别名。 :DThe correct shorthand is this:
The code you posted…
…creates an anonymous function and executes it immediately with
jQuery
being passed in as the arg$
. All it effectively does is take the code inside the function and execute it like normal, since$
is already an alias forjQuery
. :D更短的变体是使用
$
代表 jQuery,()=>{}
所谓的 '箭头函数' 从闭包继承this
。 (因此,在this
中,您可能会看到window
而不是document
。)Even shorter variant is to use
where
$
stands for jQuery and()=>{}
is so called 'arrow function' that inheritsthis
from the closure. (So that inthis
you'll probably havewindow
instead ofdocument
.)这不是
$(document).ready()
的简写。您发布的代码将内部代码装箱,并使 jQuery 可用作
$
,而不会污染全局名称空间。当您想在一个页面上同时使用原型和 jQuery 时,可以使用此方法。此处记录:http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/#use-an-immediately-invoked-function-expression
This is not a shorthand for
$(document).ready()
.The code you posted boxes the inside code and makes jQuery available as
$
without polluting the global namespace. This can be used when you want to use both prototype and jQuery on one page.Documented here: http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/#use-an-immediately-invoked-function-expression
Ready 的多框架安全简写是:
这是因为 jQuery 不是唯一使用
$
和undefined
变量的框架The multi-framework safe shorthand for ready is:
This is because jQuery isn't the only framework that uses the
$
andundefined
variables这些特定行是 jQuery 插件的常用包装:
“...为了确保您的插件不会与可能使用美元符号的其他库发生冲突,最佳实践是将 jQuery 传递给自执行函数(闭包)它将它映射到美元符号,这样它就不会被执行范围内的另一个库覆盖。”
来自 http://docs.jquery.com/Plugins/Authoring
These specific lines are the usual wrapper for jQuery plugins:
"...to make sure that your plugin doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to a self executing function (closure) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution."
From http://docs.jquery.com/Plugins/Authoring
这又如何呢?
What about this?