哪些字符对于 JavaScript 变量名有效?
哪些字符可用于命名 JavaScript 变量?
我想为工作中的非 JavaScript 用户创建一个小型“扩展库”(他们在谈到语言时似乎都有些拘谨)。我喜欢 jQuery 和 Prototype 都使用 $
美元符号,并且由于我使用 jQuery,所以我正在寻找另一个不错的单字符符号来使用。
我意识到我可以测试一些字符,但我希望缩小我的字符列表的范围(也许考虑到未来与另一个流行库的集成)。
Which characters can be used for naming a JavaScript variable?
I want to create a small "extension library" for my non-JavaScript users here at work (who all seem to be squeamish when it comes to the language). I love how jQuery and Prototype have both use the $
dollar sign, and since I use jQuery, I'm looking for another nice single-character symbol to use.
I realize that I could just test out a number of characters, but I'm hoping to narrow down my list of characters to start with (in consideration of future integration with another popular library, perhaps).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
JavaScript 变量
您可以使用任意字母、
$
或_
字符来启动变量。只要它不以数字开头,您也可以包含数字。开始:
[az], $, _
包含:
[az], [0-9], $, _
jQuery
您可以使用 < code>_ 为你的库,这样它将与 jQuery 并存。但是,您可以设置一个配置,以便 jQuery 不会使用
$
。它将改为使用jQuery
。为此,只需设置:此页面说明了如何执行此操作。
JavaScript Variables
You can start a variable with any letter,
$
, or_
character. As long as it doesn't start with a number, you can include numbers as well.Start:
[a-z], $, _
Contain:
[a-z], [0-9], $, _
jQuery
You can use
_
for your library so that it will stand side-by-side with jQuery. However, there is a configuration you can set so that jQuery will not use$
. It will instead usejQuery
. To do this, simply set:This page explains how to do this.
据我所知,接受的答案将排除许多有效的标识符。这是我整理的一个正则表达式,应遵循 spec(参见第 7.6 章有关标识符的内容)。使用 RegexBuddy 创建它,您可以在 http://samples.geekality.net/js 找到解释的导出- 标识符。
此外,该名称不能是以下保留字之一。
The accepted answer would rule out a lot of valid identifiers, as far as I can see. Here is a regular expression that I put together which should follow the spec (see chapter 7.6 on identifiers). Created it using RegexBuddy and you can find an export of the explanation at http://samples.geekality.net/js-identifiers.
In addition, the name cannot be one of the following reserved words.
如果正则表达式不是必须的,最好让浏览器决定使用
eval
:In case regular expressions is not a must, it would be better to just ask the browser to decide using
eval
:JavaScript 变量可以包含字母、数字、美元符号 ($) 和下划线 (_)。他们不能以数字开头。
通常,库使用
$
和_
作为您将在任何地方使用的函数的快捷方式。尽管名称$
或_
没有意义,但它们因其简短而很有用,并且因为您将在任何地方使用该函数,所以您应该知道它们的含义意思是。如果您的库不包括在任何地方使用单个函数,我建议您使用更有意义的名称,因为这些名称将帮助您和其他人理解您的代码在做什么,而不必损害源代码niceness< /em>.
例如,您可以查看很棒的 DateJS 库和 语法糖 它允许不需要任何符号或短命名变量。
你应该首先让你的代码实用,然后才尝试让它变得漂亮。
JavaScript variables can have letters, digits, dollar signs ($) and underscores (_). They can't start with digits.
Usually libraries use
$
and_
as shortcuts for functions that you'll be using everywhere. Although the names$
or_
aren't meaningful, they're useful for their shortness and since you'll be using the function everywhere you're expected to know what they mean.If your library doesn't consist on getting a single function being used everywhere, I'd recommend that you use more meaningful names as those will help you and others understand what your code is doing without necessarily compromising the source code niceness.
You could for instance take a look at the awesome DateJS library and at the syntactic sugar it allows without the need of any symbol or short-named variables.
You should first get your code to be practical, and only after try making it pretty.
我编写了 一个故障工作区,它迭代所有代码点并发出字符 if
eval( 'var ' + String.fromCodePoint(#) + ' = 1')
有效。它就这样一直持续着,持续着,持续着......
I wrote a glitch workspace that iterates over all the codepoints and emit the character if
eval('var ' + String.fromCodePoint(#) + ' = 1')
works.It just keeps going, and going, and going....
这是创建变量名称的一项快速建议。
如果您希望变量在 Firefox 中使用时不发生冲突,不要使用变量名称“_content”,因为该变量名称已被浏览器使用。我通过艰难的方式发现了这一点,并且不得不更改我在大型 JavaScript 应用程序中使用变量“_content”的所有位置。
Here is one quick suggestion for creating variable names.
If you want the variable not to conflict when being used in Firefox, do not use the variable name "_content" as this variable name is already being used by the browser. I found this out the hard way and had to change all of the places I used the variable "_content" in a large JavaScript application.
我采纳了Anas Nakawa 的想法并对其进行了改进。首先,没有理由实际运行所声明的函数。我们想知道它是否正确解析,而不是代码是否有效。其次,对于我们的目的来说,文字对象是比 var XXX 更好的上下文,因为它更难打破。
I've taken Anas Nakawa's idea and improved it. First of all, there is no reason to actually run the function being declared. We want to know whether it parses correctly, not whether the code works. Second, a literal object is a better context for our purpose than
var XXX
as it's harder to break out of.引用自 MDN 文档:
根据您的需要,您可能还希望排除保留标识符(其他答案已涵盖)。
Quoting from the MDN docs:
Depending on your needs, you may also want to exclude reserved identifiers (which other answers have covered).
要查看字符串 s 是否是有效的变量名,可以使用 eval 来实现。
这个想法是,我们将要测试的字符串添加到我们知道存在的对象的名称(恰好是函数本身)中,就像它是一个字段,我们将它分配给某个东西(在本例中等于零)。
如果 evals OK...则表示字符串 s 是有效的变量名,否则返回 false
To see if a string s is a valid variable name can be implemented using eval
The idea is that we add to the name of an object which we know exists (which happens to be the function itself), the string we want to test as if it was a field and we assign it to something (in this case equal to zero).
If it evals okay... it means that the string s is a valid variable name, otherwise it returns false
引用有效的 JavaScript 变量名称,我的文章总结了相关规范部分:
我还创建了 一个工具,它会告诉您是否有任何字符串您输入的是根据 ECMAScript 5.1 和 Unicode 6.1 的有效 JavaScript 变量名称:
PS 为了让您了解 Anthony Mills 的答案是多么错误:如果您将所有这些规则总结为一个仅 ASCII 的规则JavaScript 的正则表达式,长度为 11,236 个字符。这里是:
To quote Valid JavaScript variable names, my write-up summarizing the relevant spec sections:
I’ve also created a tool that will tell you if any string that you enter is a valid JavaScript variable name according to ECMAScript 5.1 and Unicode 6.1:
P.S. To give you an idea of how wrong Anthony Mills' answer is: if you were to summarize all these rules in a single ASCII-only regular expression for JavaScript, it would be 11,236 characters long. Here it is:
根据 7.6 标识符名称和标识符部分中的 ECMAScript 规范,有效标识符定义为
:为命名变量以及高尔夫运动创造了很多机会。让我们尝试一些例子。
有效的标识符可以以
UnicodeLetter
、$
、_
或\ UnicodeEscapeSequence
开头。 Unicode 字母是这些类别中的任何字符(查看所有类别) :仅此一项就解释了一些疯狂的可能性 - 工作示例。如果它不能在所有浏览器中工作,那么就将其称为错误,因为它应该工作。
From the ECMAScript specification in section 7.6 Identifier Names and Identifiers, a valid identifier is defined as:
which creates a lot of opportunities for naming variables and also in golfing. Let's try some examples.
A valid identifier could start with either a
UnicodeLetter
,$
,_
, or\ UnicodeEscapeSequence
. A Unicode letter is any character from these categories (see all categories):This alone accounts for some crazy possibilities - working examples. If it doesn't work in all browsers, then call it a bug, because it should.
基本上,采用正则表达式形式:
[a-zA-Z_$][0-9a-zA-Z_$]*
。换句话说,第一个字符可以是字母或_或$,其他字符可以是字母或_或$或数字。注意:虽然其他答案指出您可以在 JavaScript 标识符中使用 Unicode 字符,但实际问题是“我应该使用哪些字符作为 jQuery 等扩展库的名称?”这是对该问题的回答。您可以在标识符中使用 Unicode 字符,但不要这样做。编码总是会搞砸。将公共标识符保持在安全的 32-126 ASCII 范围内。
Basically, in regular expression form:
[a-zA-Z_$][0-9a-zA-Z_$]*
. In other words, the first character can be a letter or _ or $, and the other characters can be letters or _ or $ or numbers.Note: While other answers have pointed out that you can use Unicode characters in JavaScript identifiers, the actual question was "What characters should I use for the name of an extension library like jQuery?" This is an answer to that question. You can use Unicode characters in identifiers, but don't do it. Encodings get screwed up all the time. Keep your public identifiers in the 32-126 ASCII range where it's safe.
JavaScript 1.5 之前:
^[a-zA-Z_$][0-9a-zA-Z_$]*$
英文: 必须以美元符号开头,下划线或 26 个字符字母表中的字母之一(大写或小写)。后续字符(如果有)可以是其中任何一个或十进制数字。
JavaScript 1.5 及更高版本*:
^[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p {Mc}\p{Nd}\p{Pc}]*$
这用英语表达起来比较困难,但它在概念上与旧语法类似,只是字母和数字可以来自任何语言。在第一个字符之后,还允许使用其他类似下划线的字符(统称为“连接符”)和附加字符组合标记(“修饰符”)。 (此扩展集中不包含其他货币符号。)
JavaScript 1.5 及更高版本还允许 Unicode 转义序列,前提是结果是在上面的正则表达式。
标识符也不能是当前的保留字或考虑供将来使用的保留字。
标识符的长度没有实际限制。 (浏览器有所不同,但您可以安全地使用 1000 个字符,并且可能比这个数量级多几个数量级。)
字符类别链接:
(结合在上面的正则表达式中为“L”)
*nb 此 Perl 正则表达式仅用于描述语法 - 它不适用于 JavaScript,因为 JavaScript(尚)不包含对 Unicode 属性的支持。 (有一些第三方软件包声称添加了此类支持。)
Before JavaScript 1.5:
^[a-zA-Z_$][0-9a-zA-Z_$]*$
In English: It must start with a dollar sign, underscore or one of letters in the 26-character alphabet, upper or lower case. Subsequent characters (if any) can be one of any of those or a decimal digit.
JavaScript 1.5 and later * :
^[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*$
This is more difficult to express in English, but it is conceptually similar to the older syntax with the addition that the letters and digits can be from any language. After the first character, there are also allowed additional underscore-like characters (collectively called “connectors”) and additional character combining marks (“modifiers”). (Other currency symbols are not included in this extended set.)
JavaScript 1.5 and later also allows Unicode escape sequences, provided that the result is a character that would be allowed in the above regular expression.
Identifiers also must not be a current reserved word or one that is considered for future use.
There is no practical limit to the length of an identifier. (Browsers vary, but you’ll safely have 1000 characters and probably several more orders of magnitude than that.)
Links to the character categories:
(combined in the regex above as “L”)
*n.b. This Perl regex is intended to describe the syntax only — it won’t work in JavaScript, which doesn’t (yet) include support for Unicode Properties. (There are some third-party packages that claim to add such support.)
实际上,ECMAScript 在第 15 页说:
标识符可以以
$
、下划线或 UnicodeLetter 开头,然后继续(就在其下方)指定 UnicodeLetter 可以是 Unicode 类别 Lo、Ll 中的任何字符, Lu、Lt、Lm 和 Nl。当您查找这些类别时,您会发现这开辟了比拉丁字母更多的可能性。只需在 Google 中搜索“Unicode 类别”即可找到它们。
Actually, ECMAScript says on page 15:
That an identifier may start with a
$
, an underscore or a UnicodeLetter, and then it goes on (just below that) to specify that a UnicodeLetter can be any character from the Unicode categories, Lo, Ll, Lu, Lt, Lm and Nl.And when you look up those categories you will see that this opens up a lot more possibilities than just Latin letters. Just search for "Unicode categories" in Google and you can find them.