这个未知的 JavaScript 语法是什么?

发布于 2024-10-08 00:52:37 字数 231 浏览 7 评论 0原文

这段 jQuery 代码是否

(function(jQuery){
})(jQuery);

等同于

$(document).ready(function () {
});

如果是的话,两者之间有什么区别?如果没有,第一个做什么?

编辑:

谢谢大家。大多数反应相似,但口味和样品不同

Is this jQuery code

(function(jQuery){
})(jQuery);

equivalent to

$(document).ready(function () {
});

If yes, what are the differences between the two? If not, what does the first do?

EDIT:

Thanks everybody. Most response are similar with different flavours and sample

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

遗失的美好 2024-10-15 00:52:37

它们并不等同。

您的第一个示例是 立即调用函数表达式 (IIFE)。它围绕本地定义的变量创建一个闭包。

您的第二个示例指定了当 DOM 完全加载时要执行的函数已加载。它用于确保 DOM 中的所有元素节点在执行所附代码之前都可用。这也是一个闭包。

这两个示例都使用匿名函数。

值得指出的是,使用两个示例都是很好的做法,如下所示:

(function($){
    // locally-scoped, DOM-is-NOT-Ready-code here.
    $(function () {
        // your locally-scoped, DOM-is-ready-code here.
    });
}(jQuery)); // note that I've moved the invocation into the parens
            // that contain the function.  This makes JSLint happy!

They are not equivalent.

Your first example is an Immediately-Invoked Function Expression (IIFE). It creates a closure around locally defined variables.

Your second example specifies a function to execute when the DOM is fully loaded. It is used to ensure that all element nodes in the DOM are available before executing the enclosed code. This is also a closure.

Both examples use anonymous functions.

It is worth pointing out that it is good practice to use both of your examples, like so:

(function($){
    // locally-scoped, DOM-is-NOT-Ready-code here.
    $(function () {
        // your locally-scoped, DOM-is-ready-code here.
    });
}(jQuery)); // note that I've moved the invocation into the parens
            // that contain the function.  This makes JSLint happy!
神经暖 2024-10-15 00:52:37

绝对不是,第一个是自执行的匿名函数,第二个是 ready 处理程序。

(function(jQuery){
  //jQuery in this scope is referencing whatever is passed-in
})(jQuery);

因此,函数内部的 jQuery 不一定与函数外部的 jQuery 相同。但您通常不希望将全局变量名称与本地变量名称混合匹配。

举个例子:

(function(obj) {
   alert(obj);
})('Hello');

这定义了一个函数,然后立即调用它,传入“Hello”

Absolutely not, the first one is a self-executing anonymous function and the second is the ready handler.

(function(jQuery){
  //jQuery in this scope is referencing whatever is passed-in
})(jQuery);

So, the jQuery inside of the function isn't necessarily the same jQuery outside the function. But you typically do not want to mix-n-match global variable names with local ones.

Take this example:

(function(obj) {
   alert(obj);
})('Hello');

This defines a function, then immediately invokes it, passing in "Hello"

白衬杉格子梦 2024-10-15 00:52:37
$(document).ready(function () {

});

等价于:

$(function() {

});

第一个代码片段是一个立即调用的匿名函数,它创建一个本地范围:

(function() {
    var x = 2;
})();

alert(x); // undefined
$(document).ready(function () {

});

is equivalent to this:

$(function() {

});

The first snippet is an immediately invoked anonymous function which creates a local scope:

(function() {
    var x = 2;
})();

alert(x); // undefined
哀由 2024-10-15 00:52:37

不,第一个并没有真正做太多事情。只需将内部的任何变量与周围的范围分开,并在内部创建一个本地 jQuery 变量即可。

第二个传递一个函数,该函数在 DOM 准备好之后(换句话说,在 加载之后)运行。

一个常见的等价物

$(document).ready(function () {
});

是: ,

$(function () {
});

它做同样的事情。

而这个:

(function(jQuery){
})(jQuery);

经常写成:

(function($){
})(jQuery);

这样$变量就不再是全局变量了。如果全局 $ 变量已在使用中,则很有用。

No. The first one isn't really doing much. Just separating any variables inside from the surrounding scope, and creating a local jQuery variable inside.

The second one passes a function that is run after the DOM is ready (in other words after the <body> has loaded).

A common equivalent to:

$(document).ready(function () {
});

is:

$(function () {
});

which does the same thing.

While this:

(function(jQuery){
})(jQuery);

is often written as:

(function($){
})(jQuery);

so that the $ variable is no longer a global variable. Useful if the global $ variable is already in use.

奢望 2024-10-15 00:52:37

一点也不。第一个是闭包 - 您创建然后立即调用的函数。但是,通常您会像这样组合两者:


  (function($) {
    // use the $ variable
    $(document).ready(function(){
      // ...
    });
  })(jQuery);

通过创建闭包,您可以在本地将该代码块的“jQuery”重命名为“$”。使用闭包语法的原因是,即使 $ 变量可能没有在全局范围内定义为 jQuery 对象,您也可以使用它(即某些 JavaScript 框架(如原型)使用 $ 作为变量)。

每当您编写 jQuery 插件时,您都应该将所有 jQuery 代码包含在这种闭包中,这样它就不会干扰任何其他 JavaScript 框架。如果您不编写插件并且不使用任何其他 JavaScript 框架,您可能不必费心将代码包含在闭包中。

Not at all. The first one is a closure - a function that you create and then immediately call. However, typically you would combine the two like this:


  (function($) {
    // use the $ variable
    $(document).ready(function(){
      // ...
    });
  })(jQuery);

By creating the closure you are renaming "jQuery" to "$" just locally for that block of code. The reason why you use the closure syntax is so that you can use the $ variable even though it might not be defined as a jQuery object in the global scope (i.e. some JavaScript frameworks like prototype use $ as a variable).

Whenever you write a jQuery plugin you should enclose all jQuery code in this kind of closure so it doesn't interfere with any other JavaScript frameworks. If you aren't writing plugins and you don't use any other JavaScript frameworks you probably don't have to bother enclosing your code in a closure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文