函数($)在javascript中的含义是什么? [英] What does function($) mean in javascript?

查看:134
本文介绍了函数($)在javascript中的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到$只是命名指向jQuery对象的变量的约定,也是document.getElementById()的函数,但函数($)是否意味着什么?

I realize that the $ is just sort of a convention for naming variables pointing to jQuery objects, and is also the function for document.getElementById(), but does function($) mean anything?

编辑:我的意思是

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

对于困惑感到抱歉,但感谢您的答案。

Sorry for the confusion, but thanks for the answers.

推荐答案

有些代码使用$表示jQuery(或其他库)来保持全局范围的清洁。默认情况下,jQuery在全局范围内接管$,但是,如果扩展并且不能使用全局$,它可以保持范围干净,同时帮助jQuery与其他库一起工作。

Some code uses $ for jQuery (or other libraries) to keep the global scope clean. By default, jQuery takes over $ in the global scope, however, if extensions and whatnot avoid using the global $, it can keep the scope clean, along with helping jQuery work with other libraries.

(function ($) {
    //$ is now a jquery instance
})(jQuery);

基本上,这是一种使用jQuery对象即时执行代码的方法,而不依赖于全局级别的函数变量。 (也可以用它来创建闭包......但是在这种情况下,这是相同的想法。)

Basically it's a way to instantly execute code with a jQuery object without the function depending on a global-level variable. (Closures can also be created with it... But that's the same idea [in this situation].)

这篇关于函数($)在javascript中的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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