jQuery函数($)语法[duplicate]

时间:2022-10-04 22:35:49

Possible Duplicate:
jQuery: What does (function($) {})(jQuery); mean?

可能的重复:jQuery:什么(函数($){})(jQuery);的意思吗?

I stumbled up on the following code (included in one file) but I just cannot understand what it really means.

我无意中发现了以下代码(包含在一个文件中),但我就是不理解它的真正含义。

(function ($) {
    function doSomething1(somedata) {

    }

    function doSomething1(somedata) {

    }
})(jQuery);

Question 1: What does this syntax mean in the contex of jQuery

问题1:这个语法在jQuery环境中意味着什么

Question 2: How can we call these functions let say from other files such as the HTML index file and other JavaScript files?

问题2:如何调用这些函数,比如从HTML索引文件和其他JavaScript文件中调用?

Thanks

谢谢

3 个解决方案

#1


8  

This syntax is not particularly special to jquery, it's normal javascript. Here simply function

这种语法对jquery不是特别特殊,它是普通的javascript。这里简单的函数

function ($) {
    // some code here...
}

(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).

(注意,它接受名为$的参数)使用参数jQuery(显然是jQuery框架的全局对象)调用。

This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.

这通常是在一个页面上有几个js框架(jquery、dojo、prototype等)时完成的,这些框架都重新定义全局变量$。但是有了这段代码,在doSomething1或doSomething2中,您总是可以调用$('.test')并确保调用将由jquery处理,而不是dojo处理。因为在这种情况下$不是全局变量,它是函数参数。

#2


0  

I'm not sure about your question here, but the (function() means it's self-executing,

我不确定你们的问题,但是(function()表示它是自执行的,

and you call them by importing the files in the main page and then calling

通过导入主页中的文件然后调用它们

doSomething1()

doSomething1()

#3


0  

Mostly likely that was jQuery plugin: Plugins/Authoring

很可能是jQuery插件:插件/创作。

#1


8  

This syntax is not particularly special to jquery, it's normal javascript. Here simply function

这种语法对jquery不是特别特殊,它是普通的javascript。这里简单的函数

function ($) {
    // some code here...
}

(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).

(注意,它接受名为$的参数)使用参数jQuery(显然是jQuery框架的全局对象)调用。

This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.

这通常是在一个页面上有几个js框架(jquery、dojo、prototype等)时完成的,这些框架都重新定义全局变量$。但是有了这段代码,在doSomething1或doSomething2中,您总是可以调用$('.test')并确保调用将由jquery处理,而不是dojo处理。因为在这种情况下$不是全局变量,它是函数参数。

#2


0  

I'm not sure about your question here, but the (function() means it's self-executing,

我不确定你们的问题,但是(function()表示它是自执行的,

and you call them by importing the files in the main page and then calling

通过导入主页中的文件然后调用它们

doSomething1()

doSomething1()

#3


0  

Mostly likely that was jQuery plugin: Plugins/Authoring

很可能是jQuery插件:插件/创作。