Javascript $(document).ready 速记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6004129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(document).ready shorthand
提问by Mark Brown
Is the following shorthand for $(document).ready
?
以下是 的简写$(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.
我看到这种模式被大量使用,但我找不到任何参考。如果它是 的简写$(document).ready()
,是否有任何特殊原因它可能不起作用?在我的测试中,它似乎总是在就绪事件之前触发。
回答by Kyle Trauberman
The shorthand is:
简写是:
$(function() {
// Code here
});
回答by BoltClock
The shorthand for $(document).ready(handler)
is $(handler)
(where handler
is a function). See here.
的简写$(document).ready(handler)
是$(handler)
(where handler
is a function)。见这里。
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
.
您问题中的代码与.ready()
. 相反,它是一个立即调用的函数表达式 (IIFE),以 jQuery 对象作为其参数。它的目的是至少将$
变量的范围限制在它自己的块中,这样它就不会引起冲突。您通常会看到 jQuery 插件使用的模式来确保$ == jQuery
.
回答by Gordon Gustafson
The correct shorthand is this:
正确的简写是这样的:
$(function() {
// this behaves as if within document.ready
});
The code you posted…
您发布的代码...
(function($){
//some code
})(jQuery);
…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 for jQuery
. :D
...创建一个匿名函数并立即执行它并jQuery
作为 arg 传入$
。它有效地所做的只是获取函数内部的代码并像往常一样执行它,因为$
它已经是jQuery
. :D
回答by samy-delux
This is not a shorthand for $(document).ready()
.
这不是$(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.
您发布的代码封装了内部代码并使 jQuery 可用,$
而不会污染全局命名空间。当您想在一个页面上同时使用原型和 jQuery 时,可以使用它。
Documented here: http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/#use-an-immediately-invoked-function-expression
记录在这里:http: //learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/#use-an-immediately-invoked-function-expression
回答by morgar
These specific lines are the usual wrapper for jQuery plugins:
这些特定的行是 jQuery 插件的常用包装器:
"...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."
“...为了确保您的插件不会与其他可能使用美元符号的库发生冲突,最佳做法是将 jQuery 传递给一个自执行函数(闭包),该函数将其映射到美元符号,以便它可以”不会被另一个库在其执行范围内覆盖。”
(function( $ ){
$.fn.myPlugin = function() {
// Do your awesome plugin stuff here
};
})( jQuery );
回答by Timo Huovinen
The multi-framework safe shorthand for ready is:
ready 的多框架安全简写是:
jQuery(function($, undefined) {
// $ is guaranteed to be short for jQuery in this scope
// undefined is provided because it could have been overwritten elsewhere
});
This is because jQuery isn't the only framework that uses the $
and undefined
variables
这是因为 jQuery 不是唯一使用$
和undefined
变量的框架
回答by Edvard Rejthar
Even shorter variant is to use
更短的变体是使用
$(()=>{
});
where $
stands for jQuery and ()=>{}
is so called 'arrow function'that inherits this
from the closure. (So that in this
you'll probably have window
instead of document
.)
where$
代表 jQuery,也()=>{}
就是从闭包继承的所谓的“箭头函数”this
。(所以this
你可能会有window
而不是document
。)
回答by RN Kushwaha
What about this?
那这个呢?
(function($) {
$(function() {
// more code using $ as alias to jQuery
// will be fired when document is ready
});
})(jQuery);