简体   繁体   English

有人可以解释jQuery中的函数($)

[英]Can someone explain what function($) does in jQuery

Recently I was reading someone else's code, and came across this: 最近我正在读别人的代码,并且遇到了这个:

// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {

     // DOM Ready
    $(function() {
        ...
  });

})(jQuery);

I understand the point of the leading ;, And I understand that $(function() { is the same as document ready, but what is the point of adding function($)? 我理解领先的观点;,我理解$(function(){与文档准备相同,但添加函数($)的重点是什么?

I understand it's a closure, but since this is always being called at the global scope, it seems like you don't need to bother with it. 我知道它是一个闭包,但由于这个在全局范围内被调用,所以看起来你不需要为此烦恼。 The $(function() { will use the same global object either way, no? $(function(){将使用相同的全局对象,不是吗?

Is it to safeguard against something, or is it a best practice for another reason? 它是为了防范某些事情,还是出于其他原因的最佳做法?

It's a common structure for a jQuery plugin. 这是jQuery插件的常见结构。 It safeguards against the $ identifier having been overwritten and used for something else. 它可以防止已被覆盖的$标识符用于其他内容。 Inside the anonymous function, $ is always going to refer to jQuery. 在匿名函数中, $总是引用jQuery。

Example: 例:

$ = "oh no";
$(function() { //Big problem!
    //DOM ready
});

By introducing a new scope, you can ensure that $ refers to what you expect it to: 通过引入新范围,您可以确保$引用您期望的内容:

$ = "oh no";
(function($) { //New scope, $ is redeclared and jQuery is assigned to it

    $(function() { //No problem!
        //DOM ready
    }); 

}(jQuery));

The main reasoning behind this is that numerous other JavaScript libraries use $ as an identifier (eg PrototypeJS). 其背后的主要原因是许多其他JavaScript库使用$作为标识符(例如PrototypeJS)。 If you wanted to use both Prototype and jQuery, you need to let Prototype have its $ identifier, but you probably don't want to write out jQuery every time you want to call a jQuery method. 如果你想同时使用原型和jQuery,你需要让原型有其$标识符,但你可能不希望写出来jQuery你要调用一个jQuery方法每次。 By introducing a new scope you allow jQuery to have its $ back in that execution context. 通过引入新的范围,你让jQuery来有其$在执行上下文回来。

The code sample you've provided is an example of a Self-Invoking Function: 您提供的代码示例是自调用函数的示例:

(function(){
 // some code…
})();

The first set of parentheses defines a function: (an anonymous function wrapped in parentheses) 第一组括号定义了一个函数:(括在括号中的匿名函数)

(function() {})

That defines the anonymous function. 这定义了匿名函数。 On its own, it doesn't do anything. 就其本身而言,它什么都不做。 But if you add a set of parentheses () after the definition, it's the same as the parentheses used to call a function. 但是如果在定义之后添加一组括号() ,它与用于调用函数的括号相同。 Try this out: 试试这个:

(function(message) {
  alert(message);
})("Hello World");

That creates a function which accepts a parameter, and displays an alert box containing the provided value. 这将创建一个接受参数的函数,并显示一个包含所提供值的警告框。 Then, it immediately calls that function with a parameter of "Hello World". 然后,它立即使用参数“Hello World”调用该函数。


In your example, a self-invoking function is defined. 在您的示例中,定义了一个自调用函数。 It accepts a parameter, which is named $ . 它接受一个名为$的参数。 Then, the function is immediately called, with a reference to jQuery being passed in as the argument. 然后,立即调用该函数,并引用jQuery作为参数传入。

This is common if you want jQuery to operate in noConflict() mode (which removes the global reference to $ ). 如果您希望jQuery在noConflict()模式下运行(这会删除对$的全局引用),这很常见。

In noConflict() mode, you can still access jQuery via the jQuery global variable, but most people would rather use $ , so this self-calling function accepts the global jQuery variable as a parameter named $ in the scope of the function, which leaves you free to use the $ shortcut within the self-invoking function while having jQuery operate in noConflict() mode to avoid clashes with other libraries that use $ in the global scope. noConflict()模式下,你仍然可以通过jQuery全局变量访问jQuery,但是大多数人宁愿使用$ ,所以这个自调用函数接受全局jQuery变量作为函数范围内名为$的参数,您可以自由地使用自调用函数中的$快捷方式,同时让jQuery在noConflict()模式下运行,以避免与在全局范围内使用$其他库发生冲突。

Hope this answers your question! 希望这能回答你的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM