jQuery的功能($)(jQuery的)语法 [英] jQuery's function($) (jQuery) syntax

查看:142
本文介绍了jQuery的功能($)(jQuery的)语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我无意中发现了以下code(包含在一个文件中),但我只是不明白它的真正含义。

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

问题1:
这是什么意思语法jQuery中的CONTEX

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

问题2:
我们怎样才能调用这些功能让其他文件中说,如HTML索引文件和其他JavaScript文件?

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

感谢

推荐答案

这语法是不是特别特别的jQuery的,这是正常的JavaScript。这里简单功能

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

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

(请注意,它需要参数命名 $ )被调用,参数的jQuery (这是很明显,全球jQuery框架的目标)。

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

在一个页面(jQuery的,道场,原型等),所有重新定义全局变量 $ 上有几个是框架JS这通常是。但有了这个code,在 doSomething1 了doSomething2 您可以随时拨打 $( .TEST')键,可以肯定,通话将被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.

这篇关于jQuery的功能($)(jQuery的)语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆