- 浏览: 340256 次
- 性别:
- 来自: 北京
文章分类
最新评论
一直不知道为什么那么多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: _
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?
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:
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时才用它,其他时候不要用,虽然在效果上没有差别。感觉这是因为框架中有不少全局变量,容易起冲突,这就需要让它的名称特殊一点,而$的出现频率相较而言还是比较低的,把它用于全局变量的名称就是个很好的选择了。
欢迎拍砖!
转自: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时才用它,其他时候不要用,虽然在效果上没有差别。感觉这是因为框架中有不少全局变量,容易起冲突,这就需要让它的名称特殊一点,而$的出现频率相较而言还是比较低的,把它用于全局变量的名称就是个很好的选择了。
欢迎拍砖!
发表评论
-
涉及页面展示尽量少用js
2012-07-04 10:42 1309用js操作DOM元素会引起页面频繁的reflow和 ... -
inline Element相互之间多出几像素的空白
2011-10-19 05:17 2388若干个行内元素并排排列,margin,padding都设成0了 ... -
兼容各浏览器盒子模型
2011-09-24 23:36 5795各浏览器盒模型的 ... -
获取鼠标相对位置
2011-09-20 10:48 2496获取鼠标相对于html的位置: function mo ... -
判断日期格式
2011-08-17 09:34 1096var startTime = "2011-03-3 ... -
JS简易拖拽效果
2011-07-31 22:48 6900模仿《javascript权威指南》写了个简易拖拽程序,麻雀虽 ... -
IE事件注册相关的内存泄露总结
2011-06-03 17:28 1444经过一上午的艰苦试验验证,得出如下结论: 1、将元 ... -
假的,全是假的!!(更新)
2010-10-11 21:31 1490<meta http-equiv="P ... -
IE内存泄露与无法回收研究小结(持续增加中)
2010-09-21 16:19 6291一、内存泄露 之前 ... -
javascript父、子页面交互小结
2010-06-04 12:05 8551帧用来存放子页面 ... -
获取子窗口中使用jQuery.data()设置的参数
2010-04-13 17:49 5589假设在iframe子窗口中设置了$('#mydata') ... -
JS 超类和子类
2010-03-30 19:52 5497先引一段原文,可略过.. .....Object is ... -
《JavaScript The Definitive Guide 5th》闭包读后总结
2010-03-24 10:51 1386JavaScript functions are a comb ... -
"position:relative"在IE中的Bug
2010-03-22 09:29 6040请看下图: 即子元素过高导致父元素出现滚动条时,它并不会像预 ... -
JS没有"块作用域"
2010-03-17 16:01 1423没别的意思,只是记下来而已,这在犀牛书上是明明白白地写着 ... -
jquery的data函数
2010-02-03 18:45 3340今天试着使用了下这个函数,真是万事开头难,碰了个灰头土脸, ... -
使用设计模式的目的
2009-12-09 10:24 2050为什么要用设计模式?在生搬硬套了策略模式之后产生了这种疑 ... -
jquery 整体感觉
2009-11-12 10:34 1337用了一段时间后,觉得它和Ext虽然有很大不同,但如果稍微 ... -
jquery的each函数中的break和continue功能
2009-10-20 17:06 27363each函数不能使用break和continue关键字,替代方 ... -
移除JS数组指定位置的元素
2009-10-15 10:34 18922JavaScript中splice函数方法是从一个数组 ...
相关推荐
总结来说,在使用jQuery时,如果发现代码中的特效没有按预期工作,应当考虑是否引入了其他使用$符号的JavaScript库导致冲突。检查引入库的顺序和使用IIFE的局部作用域来解决冲突都是有效的策略。此外,为了避免未来...
在JavaScript编程语言中,`$` 和 `$$` 符号具有不同的含义和用途,尤其在不同的上下文和库中。下面将详细解释这两个符号以及它们的区别。 首先,`$` 符号在JavaScript中最常见的用途是作为jQuery库的主函数。jQuery...
在编程世界里,$符号是一种常见的特殊字符,它在不同的编程语言中有着不同的含义和用法。这篇文字段落主要探讨了$符号在shell脚本、makefile、jQuery和PHP中的角色。 1. **Shell脚本** 在Unix/Linux系统的shell...
本篇文章将详细讲解如何在SuperMap iClient 6R中结合JavaScript实现关联外表的等级符号专题图。 首先,**关联外表**是GIS中的一个重要概念,它允许我们将非空间数据(如人口统计、经济指标等)与空间数据(如地理...
在JavaScript编程语言中,`$`美元符号是一个常见的标识符,尤其在jQuery和其他JavaScript库中广泛使用。这个符号被用作一个简化的选择器,类似于CSS中的选择器,用于选取DOM(Document Object Model)中的元素。`$`...
在JavaScript和jQuery的世界中,`$`符号是一个非常特殊的字符,它被广泛用作jQuery库的别名,方便开发者编写简洁、高效的代码。然而,当一个页面中同时引用了多个版本的jQuery或者其他使用了`$`作为主要函数的...
JavaScript在此过程中起到关键作用,它是实现跨平台交互和通用符号化的核心技术。通过JavaScript,可以编写跨浏览器、跨系统的代码,实现对地图符号的动态生成和更新,确保在不同GIS环境下的一致性。此外,...
JavaScript中的军事符号_JavaScript_下载.zip
在讨论JavaScript中美元符号$的含义时,首先要明确它在不同编程语言中的不同用途。在PHP中,美元符号$通常用作变量的标识符。而在JavaScript中,$符号的用途则多样化,且深受一些JavaScript框架和库的影响。 1. ...
在JavaScript编程语言中,去除字符串中的特殊字符及标点符号是一项常见的需求,尤其是在处理用户输入、文本解析或数据清洗等场景下。本文将详细介绍如何利用正则表达式及其他方法实现这一功能,并探讨其应用场景与...
在URL中传递参数时,有些特殊符号由于是URL的保留字符,不能直接使用,否则可能会导致参数解析错误或者行为不符合预期。这些特殊字符包括但不限于:`&`、`=`、`%`、`#` 等。 为了在URL中传递这些特殊字符,我们需要...
在使用IntelliJ IDEA(IDEA)开发JavaScript项目时,有时会遇到jQuery代码中的`$`符号被标记为黄色波浪线,表示IDE无法识别该符号。这种情况通常是由于IDEA缺少对jQuery库的识别或者相应的插件未正确配置所致。为了...
ArcGIS API for JavaScript还支持自定义图件和符号,使地图的视觉效果更加丰富。通过SVG、Canvas或WebGL技术,可以创建复杂的图件样式,增强地图的视觉吸引力。另外,事件监听(Event Handling)也是API的一个重要...
然而,当我们引入多个js库后,在另外一个js库中也定义了$符号的话,那么我们在使用$符号时就发生了冲突。下面以引入两个库文件 jquery.js和prototype.js为例来进行说明。 第一种情况:jquery.js在prototype.js之后...
JavaScript内存数据做等级符号专题图是一种在WebGIS应用中常用的数据可视化方法,它结合了SuperMap iClient JavaScript库的功能,用于高效地展示地理空间数据。本文将深入探讨这个专题图的实现原理、特点以及如何...
对象是JavaScript的核心,它们由键值对组成,键通常是字符串,但也可以是符号。对象可以通过字面量语法创建,或者通过构造函数。原型链是JavaScript实现继承的一种方式,每个对象都有一个proto属性指向其构造函数的...
"03 表达式与运算符.pdf"涉及了JavaScript中的算术、比较、逻辑及赋值运算符。表达式是能够产生值的语言结构,而运算符则是对这些值进行操作的符号。了解各种运算符的工作原理可以帮助开发者编写出更加高效和精确的...
另外,它强调了易用性,并且特别指出它不依赖于JavaScript,这意味着它可以作为一个轻量级的解决方案来提供表情符号服务,无需在网页或应用中加载额外的JavaScript库。 **描述分析:** 描述进一步巩固了标题中的...
* 函数是JavaScript中的一种基本结构单元 * 函数可以封装一组语句,实现功能的复用 * 函数可以传递参数,实现参数的传递 * 函数可以返回值,实现结果的返回 三、 JavaScript 中的对象 * 对象是JavaScript中的一种...
1. **基本语法**:JavaScript语法基于ECMAScript规范,包括变量声明(var、let、const)、数据类型(如字符串、数字、布尔值、null、undefined、对象、数组、符号、大整数)、运算符(算术、比较、逻辑、位、赋值等...