javascript 自调用函数jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17319718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:52:38  来源:igfitidea点击:

Self-invoking function jQuery

javascriptjqueryself-invoking-function

提问by Sumurai8

I noticed that in some places, jQuery code is wrapped in a self-invoking function like below. Why is this done, in what cases is this useful and in what cases is an unnecessary boilerplate?

我注意到在某些地方,jQuery 代码包含在如下所示的自调用函数中。为什么要这样做,在什么情况下这是有用的,在什么情况下是不必要的样板?

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

回答by Precastic

The short answer: To prevent variable name conflicts. It is not always needed, but good practice in order to create conflict free reusable code.

简短的回答:防止变量名冲突。它并不总是需要的,但是为了创建无冲突的可重用代码,这是一种很好的做法。

The long answer: In javascript the $ symbol is just another variable. jQuery uses it because it is a nice shorthand instead of having to type out jQuery each time, but so can any other code (including other libraries).

长答案:在 javascript 中,$ 符号只是另一个变量。jQuery 使用它是因为它是一个很好的速记,而不必每次都输入 jQuery,但任何其他代码(包括其他库)也可以。

To prevent conflict with other uses of a variable at the same scope (in this case $ at the "global" scope) it is common for code to be wrapped in a self-invoking function with the "conflict-free" variables passed as the function parameters. This then creates a new scope where your variable won't interfere with other uses of that variable. That way you can pass the full named variable & uses it with whatever name you want within the anonymous function.

为了防止与同一范围内变量的其他使用(在本例中为“全局”范围内的 $ )发生冲突,通常将代码包装在自调用函数中,并将“无冲突”变量作为函数参数。然后这会创建一个新的作用域,您的变量不会干扰该变量的其他用途。这样你就可以传递完整的命名变量并在匿名函数中使用你想要的任何名称。

So, when creating conflict free reusable code it is good practice to use this methodology:

因此,在创建无冲突的可重用代码时,最好使用这种方法:

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

Along these lines you will also see the following format:

沿着这些行,您还将看到以下格式:

(function( $, window, undefined ) {
  ...
})( jQuery, window );

In this instance undefinedis simply used for readability to indicating that no more arguments are passed to the function.

在此实例undefined中仅用于可读性以指示不再向函数传递参数。

回答by Mohammad Adil

In case you want to avoid conflict regarding use of $

如果您想避免有关使用的冲突 $

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

Inside this function you can use $without having to worry about it's use outside of it as inside that function, $always refers to the jQueryobject.

在这个函数内部你可以使用$而不必担心它在它外部的使用就像在那个函数内部一样,$总是指jQuery对象。

This is helpful while creating jQuery plugins,You will see jQuery plugin's use this kind of function to avoid conflicts with other plugins.

这在创建 jQuery 插件时很有帮助,您会看到 jQuery 插件使用这种功能来避免与其他插件冲突。

Reference :http://learn.jquery.com/plugins/basic-plugin-creation/

参考:http://learn.jquery.com/plugins/basic-plugin-creation/

回答by YD1m

In function scope $is local variable that not conflict with any other global $.

在函数作用域中$是局部变量,不与任何其他全局变量冲突$