$(document).ready 简写

发布于 2024-11-07 06:20:15 字数 225 浏览 0 评论 0原文

以下是 $(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

花海 2024-11-14 06:20:15

简写是:

$(function() {
    // Code here
});

The shorthand is:

$(function() {
    // Code here
});
北座城市 2024-11-14 06:20:15

$(document).ready(handler) 的简写形式是 $(handler) (其中 handler 是一个函数)。请参阅此处

您问题中的代码与 .ready() 无关。相反,它是一个以 jQuery 对象作为参数的立即调用函数表达式 (IIFE)。其目的是将至少 $ 变量的范围限制在其自己的块中,这样就不会引起冲突。您通常会看到 jQuery 插件使用的模式来确保 $ == jQuery

The shorthand for $(document).ready(handler) is $(handler) (where handler 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.

一绘本一梦想 2024-11-14 06:20:15

正确的简写是这样的:

$(function() {
    // this behaves as if within document.ready
});

您发布的代码...

(function($){

//some code

})(jQuery);

...创建一个匿名函数并立即执行它,并将 jQuery 作为参数 $ 传入。它有效做的就是获取函数内的代码并像平常一样执行它,因为 $ 已经是 jQuery 的别名。 :D

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

独自唱情﹋歌 2024-11-14 06:20:15

更短的变体是使用

$(()=>{

});

$ 代表 jQuery,()=>{} 所谓的 '箭头函数' 从闭包继承 this 。 (因此,在 this 中,您可能会看到 window 而不是 document。)

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.)

定格我的天空 2024-11-14 06:20:15

这不是 $(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

热鲨 2024-11-14 06:20:15

Ready 的多框架安全简写是:

jQuery(function($, undefined) {
    // $ is guaranteed to be short for jQuery in this scope
    // undefined is provided because it could have been overwritten elsewhere
});

这是因为 jQuery 不是唯一使用 $undefined 变量的框架

The multi-framework safe shorthand for ready is:

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

诗笺 2024-11-14 06:20:15

这些特定行是 jQuery 插件的常用包装:

“...为了确保您的插件不会与可能使用美元符号的其他库发生冲突,最佳实践是将 jQuery 传递给自执行函数(闭包)它将它映射到美元符号,这样它就不会被执行范围内的另一个库覆盖。”

(function( $ ){
  $.fn.myPlugin = function() {
    // Do your awesome plugin stuff here
  };
})( 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."

(function( $ ){
  $.fn.myPlugin = function() {
    // Do your awesome plugin stuff here
  };
})( jQuery );

From http://docs.jquery.com/Plugins/Authoring

英雄似剑 2024-11-14 06:20:15

这又如何呢?

(function($) { 
   $(function() {
     // more code using $ as alias to jQuery
     // will be fired when document is ready
   });
})(jQuery);

What about this?

(function($) { 
   $(function() {
     // more code using $ as alias to jQuery
     // will be fired when document is ready
   });
})(jQuery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文