`
hyj1254
  • 浏览: 341089 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaScript中的$符号

阅读更多
  一直不知道为什么那么多JS代码里面都用到了$符号,查了下,外文的。虽然很长,但还是建议看一遍,讲得通俗易懂,又不失深度,值得一看。文章最后是我的总结,请指正。
   转自:http://www.authenticsociety.com/blog/JavaScript_DollarSign
      Javascript Dollar Sign. A beginner, or even a seasoned JavaScript programmer may be slightly intimidated once spotting something as ambiguous as the dollar sign ($) in JavaScript code for the first time, without reading any books about JavaScript and jQuery. To explain the usage of the dollar sign in JavaScript code, I need to lay out some generics. In JavaScript, the dollar sign commonly appears in variable definitions and function calls. Let me assure you that there is nothing mysterious about the dollar sign, for it is just a variable (or an identifier) name. There is nothing Greek about it. As an example of this, the insanely popular JavaScript framework, about which I had previously written an article (What is jQuery?) uses the dollar sign to instantiate the main jQuery object.
In many computer programming languages or script languages a variable is also referred to as an identifier. Each programming language has a design whether it be C, C++, PHP, Java or Javascript. Each language design also has a set of rules. For example, in JavaScript the rules for defining variables is that each identifier must start with a letter, the dollar sign ($) or the underscore character (_) and must never start with a digit (such as 0-1) or some of the other characters such as punctuation signs and a few others (because that would confuse the crap out of the compiler). Both of these symbols ($ and _) are special cases and may also appear in the rest of the identifier name. So, as an example, a variable identifier named by five consequent dollar signs such as $$$$$ (or five, or another number of underscores in a row, for that matter) is totally acceptable because it falls within the rules of the JavaScript language syntax. This is simply a language rule that JavaScript programmers must live with. And believe me, there are very good reasons for that.
Once upon a time, there was a global function object, whose identifier's name was a single dollar sign $. This coding practice (lack of the var keyword) was undesirable by the skilled and the aged programmers, because we do not favor global variables in our JavaScript code unless we are cheating. However, the important part is that this function could have been named almost anything such as: a, z or even a single underscore: _
// An example of legal identifier names
var A = function() {
    alert("function A was called");
}
var $ = function() {
    alert("function $ was called");
}
var _ = function() {
    alert("function _ was called");
}

Furthermore, in addition to the dollar sign and the underscore character, in JavaScript version 1.5 and later, you can also use ISO 8859-1 and Unicode letters such as ?to name your identifiers. Surprisingly for some, one can also use the uXXXX Unicode escape sequences where XXXX would be a number such as 0024. What is interesting is that the unicode u0024 escape sequence translates to, guses what?... the dollar sign. And it is even possible to call the function, literally defined with identifier named u0024 by refering to it with the dollar sign character! Of course, just because this is possible, it is very uncommon to do this in your code. I consider this an undesirable practice, because not only many programmers are not aware of this, if you use these "techniques" the code starts to look confusing and unreadable.
As you may know, JavaScript is an object-oriented programming language by design. There are several different ways to assign a value to an identifier. For example, when we use the keyword var, the JavaScript will create a variable in the current (or local) scope of the code block you are adding the code to. If we skip the var keyword, the variable will still be created, but in the global scope of your program, which means it would be visible from anywhere in the (.js) file. Again, try to avoid using globals. I know this is a page about the dollar sign, but if it is not yet obvious to you, the no-globals rule will surface once you work with large-scale projects, multiple components written by someone else and/or in a team of a few other software engineers. So if someone tells you it is okay to use globals, and create variables without the var keyword - don't believe their lies.
One more thing. Because JavaScript variables are objects by design you can assign functions to your variables. You can even assign "member" functions to functions that already exist. But by trying to assign a function to a function object that does not yet exist will not compile in JavaScript. If you really want to get in depth with this, I will ask you to pay careful attention to the code shown below.
Now, with the knowledge you've gained from this article, and without copy and pasting the code to test it in your browser, can you tell whether the following code will compile?
<script type = "text/javascript">
window.onload = function()
{
    // Define function objects
    var func1   = function() { alert("hello from func1"); }
    func1.func2 = function() { alert("hello from func1.func2"); };
    var _       = function() { alert("hello from _"); }
    var \u0024  = function() { alert("hello from $ defined as u0024"); }
    var ?      = function() { alert("hello from ?); }
    var $$$$$   = function() { alert("hello from $$$$$"); }
    var $func$  = function() { alert("hello from $func$"); }
    var __      = function() { alert("hello from __"); }
    _.$         = function() { alert("hello from _.$"); }
    __.__       = function() { alert("hello from __.__"); }
    $.member    = function() { alert("hello from $.member"); }
    // Call functions defined above one by one
    func1();
    func1.func2();
    _();
    $();
    ?);
    $$$$$();
    $func$();
    $.member();
    _.$();
    __();
    __.__();
}
</script>

y convention, the dollar sign ($), the underscore (_) and even some ASCII character are permitted to be used anywhere in a JavaScript identifier (Source: Ecma Script documentation (7.6 Identifiers, ECMA-262, 3rd Ed.) the dollar sign is intended for use only in mechanically generated code. This means that we do not want to use the dollar sign ($) in our indentifier names, unless we are writing a framework. The following is a list of permitted characters that can be used anywhere in an identifier name:
     IdentifierName ::
    IdentifierStart
    IdentifierName IdentifierPart
    IdentifierStart ::
    UnicodeLetter
    $
    _
    UnicodeEscapeSequence
    IdentifierPart ::
    IdentifierStart
    UnicodeCombiningMark
    UnicodeDigit
    UnicodeConnectorPunctuation
    UnicodeEscapeSequence

The point of this article is to show you that the dollar sign character (implemented by the popular js framework called Prototype and then picked up by the jQuery developers) is simply an identifier name, nothing more. Why is it used by programmers? Well, it is a very convenient way to name a function that has a distinct purpose in framework code. And this is the reason jQuery's main object, defined as the dollar sign, can be used interchangeably with the actual object whose name is jQuery. We do not use $ in our custom code, we use the jQuery object. If you are familiar with jQuery and you have paid close attention to its documentation, using the $ alias for the actual object named jQuery in your jQuery plugin code is not desired, although, for some reason it is very common among programmers who find it cute.

Dollar Sign ($) Usage Summary

The dollar sign usage is semantics. It is an identifier name. Psychologically, it looks cute and convenient. Internally, it is just a function or an alpha-numeric variable reference. But it really makes no difference whether you use $ or _ or any other character as explained in the article on my website linked to from this page by the author of this page, for which I thank him.
The usage of the $, as pointed out by the Ecmascript specification, is a character that is allowed to be contained in a variable identifier name. Using the $ as the only character in an identifier name sure does make it look strange, but the difference is semantic in nature. As far as I know, doing so is no different than using any other character such as a, or b in an identifier name.
Again, Ecmascript specification documentation tells us that the $ should be used by internal code such as the jQuery framework simply out of convenience. So this means we should not use it in our code just because it looks cool, unless we are programming a framework that can really benefit from it, because it makes the global framework object unique and separate from the rest of the code.

   我的理解:
   "$"不过是JavaScript的一个可以用来命名变量的普通标识符而已,和其他字符在使用效果上没有任何差别。可以写var book;也可以写var b$$k;或var $book;
    但是,JS推荐只在写框架如JQuery时才用它,其他时候不要用,虽然在效果上没有差别。感觉这是因为框架中有不少全局变量,容易起冲突,这就需要让它的名称特殊一点,而$的出现频率相较而言还是比较低的,把它用于全局变量的名称就是个很好的选择了。

    欢迎拍砖!
分享到:
评论

相关推荐

    jQuery解决$符号命名冲突

    总结来说,在使用jQuery时,如果发现代码中的特效没有按预期工作,应当考虑是否引入了其他使用$符号的JavaScript库导致冲突。检查引入库的顺序和使用IIFE的局部作用域来解决冲突都是有效的策略。此外,为了避免未来...

    JavaScript中的$与$$是什么意思? 有什么区别.zip

    在JavaScript编程语言中,`$` 和 `$$` 符号具有不同的含义和用途,尤其在不同的上下文和库中。下面将详细解释这两个符号以及它们的区别。 首先,`$` 符号在JavaScript中最常见的用途是作为jQuery库的主函数。jQuery...

    各种编程语言中$符号的意义

    在编程世界里,$符号是一种常见的特殊字符,它在不同的编程语言中有着不同的含义和用法。这篇文字段落主要探讨了$符号在shell脚本、makefile、jQuery和PHP中的角色。 1. **Shell脚本** 在Unix/Linux系统的shell...

    SuperMap iClient 6R for JavaScript关联外表做等级符号专题图.

    本篇文章将详细讲解如何在SuperMap iClient 6R中结合JavaScript实现关联外表的等级符号专题图。 首先,**关联外表**是GIS中的一个重要概念,它允许我们将非空间数据(如人口统计、经济指标等)与空间数据(如地理...

    JavaScript中也使用$美元符号来代替document.getElementById

    在JavaScript编程语言中,`$`美元符号是一个常见的标识符,尤其在jQuery和其他JavaScript库中广泛使用。这个符号被用作一个简化的选择器,类似于CSS中的选择器,用于选取DOM(Document Object Model)中的元素。`$`...

    jQuery中 $ 符号的冲突问题及解决方案

    在JavaScript和jQuery的世界中,`$`符号是一个非常特殊的字符,它被广泛用作jQuery库的别名,方便开发者编写简洁、高效的代码。然而,当一个页面中同时引用了多个版本的jQuery或者其他使用了`$`作为主要函数的...

    基于Javascript的GIS符号化技术研究.pdf

    JavaScript在此过程中起到关键作用,它是实现跨平台交互和通用符号化的核心技术。通过JavaScript,可以编写跨浏览器、跨系统的代码,实现对地图符号的动态生成和更新,确保在不同GIS环境下的一致性。此外,...

    JavaScript中的军事符号_JavaScript_下载.zip

    JavaScript中的军事符号_JavaScript_下载.zip

    js程序中美元符号$是什么

    在讨论JavaScript中美元符号$的含义时,首先要明确它在不同编程语言中的不同用途。在PHP中,美元符号$通常用作变量的标识符。而在JavaScript中,$符号的用途则多样化,且深受一些JavaScript框架和库的影响。 1. ...

    javascript 去除特殊符和标点符号

    在JavaScript编程语言中,去除字符串中的特殊字符及标点符号是一项常见的需求,尤其是在处理用户输入、文本解析或数据清洗等场景下。本文将详细介绍如何利用正则表达式及其他方法实现这一功能,并探讨其应用场景与...

    javaScript处理URL中特殊符号的处理

    在URL中传递参数时,有些特殊符号由于是URL的保留字符,不能直接使用,否则可能会导致参数解析错误或者行为不符合预期。这些特殊字符包括但不限于:`&`、`=`、`%`、`#` 等。 为了在URL中传递这些特殊字符,我们需要...

    IDEA配置jQuery, $符号不再显示黄色波浪线的问题

    在使用IntelliJ IDEA(IDEA)开发JavaScript项目时,有时会遇到jQuery代码中的`$`符号被标记为黄色波浪线,表示IDE无法识别该符号。这种情况通常是由于IDEA缺少对jQuery库的识别或者相应的插件未正确配置所致。为了...

    arcgis api for javascript 中文帮助文档和demo

    ArcGIS API for JavaScript还支持自定义图件和符号,使地图的视觉效果更加丰富。通过SVG、Canvas或WebGL技术,可以创建复杂的图件样式,增强地图的视觉吸引力。另外,事件监听(Event Handling)也是API的一个重要...

    JavaScript中文参考手册

    2. **函数**:JavaScript中的函数是第一类对象,可以作为参数传递,也可以作为返回值。函数表达式、函数声明、箭头函数和作用域规则都是重要的部分。 3. **对象和原型**:JavaScript采用基于原型的继承机制,对象...

    解决jquery中美元符号命名冲突问题

    然而,当我们引入多个js库后,在另外一个js库中也定义了$符号的话,那么我们在使用$符号时就发生了冲突。下面以引入两个库文件 jquery.js和prototype.js为例来进行说明。 第一种情况:jquery.js在prototype.js之后...

    JavaScript内存数据做等级符号专题图

    JavaScript内存数据做等级符号专题图是一种在WebGIS应用中常用的数据可视化方法,它结合了SuperMap iClient JavaScript库的功能,用于高效地展示地理空间数据。本文将深入探讨这个专题图的实现原理、特点以及如何...

    javascript 中文帮助文档

    对象是JavaScript的核心,它们由键值对组成,键通常是字符串,但也可以是符号。对象可以通过字面量语法创建,或者通过构造函数。原型链是JavaScript实现继承的一种方式,每个对象都有一个proto属性指向其构造函数的...

    javascript中文参考资料

    "03 表达式与运算符.pdf"涉及了JavaScript中的算术、比较、逻辑及赋值运算符。表达式是能够产生值的语言结构,而运算符则是对这些值进行操作的符号。了解各种运算符的工作原理可以帮助开发者编写出更加高效和精确的...

    emojiawesome跨平台易于使用没有javascript的emojis表情符号

    另外,它强调了易用性,并且特别指出它不依赖于JavaScript,这意味着它可以作为一个轻量级的解决方案来提供表情符号服务,无需在网页或应用中加载额外的JavaScript库。 **描述分析:** 描述进一步巩固了标题中的...

    JavaScript学习笔记,javascript基础知识,基础语法整理.pdf

    * 函数是JavaScript中的一种基本结构单元 * 函数可以封装一组语句,实现功能的复用 * 函数可以传递参数,实现参数的传递 * 函数可以返回值,实现结果的返回 三、 JavaScript 中的对象 * 对象是JavaScript中的一种...

Global site tag (gtag.js) - Google Analytics