What characters are valid for JavaScript variable names?
Can anyone point me to a Web page detailing the characters that 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 posing this question to the many JavaScript sensei of the world for advice about what characters (even if valid) would be a bad idea to use (in consideration of future integration with another popular library, perhaps).
To quote Valid JavaScript variable names , my write-up summarizing the relevant spec sections:
An identifier must start with $
, _
, or any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.
The rest of the string can contain the same characters, plus any U+200C zero width non-joiner characters, U+200D zero width joiner characters, and characters in the Unicode categories “Non-spacing mark (Mn)”, “Spacing combining mark (Mc)”, “Decimal digit number (Nd)”, or “Connector punctuation (Pc)”.
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:
PS 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:
// ES5.1 / Unicode 6.1
/^(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[$A-Z_a-zxaaxb5xbaxc0-xd6xd8-xf6xf8-u02c1u02c6-u02d1u02e0-u02e4u02ecu02eeu0370-u0374u0376u0377u037a-u037du0386u0388-u038au038cu038e-u03a1u03a3-u03f5u03f7-u0481u048a-u0527u0531-u0556u0559u0561-u0587u05d0-u05eau05f0-u05f2u0620-u064au066eu066fu0671-u06d3u06d5u06e5u06e6u06eeu06efu06fa-u06fcu06ffu0710u0712-u072fu074d-u07a5u07b1u07ca-u07eau07f4u07f5u07fau0800-u0815u081au0824u0828u0840-u0858u08a0u08a2-u08acu0904-u0939u093du0950u0958-u0961u0971-u0977u0979-u097fu0985-u098cu098fu0990u0993-u09a8u09aa-u09b0u09b2u09b6-u09b9u09bdu09ceu09dcu09ddu09df-u09e1u09f0u09f1u0a05-u0a0au0a0fu0a10u0a13-u0a28u0a2a-u0a30u0a32u0a33u0a35u0a36u0a38u0a39u0a59-u0a5cu0a5eu0a72-u0a74u0a85-u0a8du0a8f-u0a91u0a93-u0aa8u0aaa-u0ab0u0ab2u0ab3u0ab5-u0ab9u0abdu0ad0u0ae0u0ae1u0b05-u0b0cu0b0fu0b10u0b13-u0b28u0b2a-u0b30u0b32u0b33u0b35-u0b39u0b3du0b5cu0b5du0b5f-u0b61u0b71u0b83u0b85-u0b8au0b8e-u0b90u0b92-u0b95u0b99u0b9au0b9cu0b9eu0b9fu0ba3u0ba4u0ba8-u0baau0bae-u0bb9u0bd0u0c05-u0c0cu0c0e-u0c10u0c12-u0c28u0c2a-u0c33u0c35-u0c39u0c3du0c58u0c59u0c60u0c61u0c85-u0c8cu0c8e-u0c90u0c92-u0ca8u0caa-u0cb3u0cb5-u0cb9u0cbdu0cdeu0ce0u0ce1u0cf1u0cf2u0d05-u0d0cu0d0e-u0d10u0d12-u0d3au0d3du0d4eu0d60u0d61u0d7a-u0d7fu0d85-u0d96u0d9a-u0db1u0db3-u0dbbu0dbdu0dc0-u0dc6u0e01-u0e30u0e32u0e33u0e40-u0e46u0e81u0e82u0e84u0e87u0e88u0e8au0e8du0e94-u0e97u0e99-u0e9fu0ea1-u0ea3u0ea5u0ea7u0eaau0eabu0ead-u0eb0u0eb2u0eb3u0ebdu0ec0-u0ec4u0ec6u0edc-u0edfu0f00u0f40-u0f47u0f49-u0f6cu0f88-u0f8cu1000-u102au103fu1050-u1055u105a-u105du1061u1065u1066u106e-u1070u1075-u1081u108eu10a0-u10c5u10c7u10cdu10d0-u10fau10fc-u1248u124a-u124du1250-u1256u1258u125a-u125du1260-u1288u128a-u128du1290-u12b0u12b2-u12b5u12b8-u12beu12c0u12c2-u12c5u12c8-u12d6u12d8-u1310u1312-u1315u1318-u135au1380-u138fu13a0-u13f4u1401-u166cu166f-u167fu1681-u169au16a0-u16eau16ee-u16f0u1700-u170cu170e-u1711u1720-u1731u1740-u1751u1760-u176cu176e-u1770u1780-u17b3u17d7u17dcu1820-u1877u1880-u18a8u18aau18b0-u18f5u1900-u191cu1950-u196du1970-u1974u1980-u19abu19c1-u19c7u1a00-u1a16u1a20-u1a54u1aa7u1b05-u1b33u1b45-u1b4bu1b83-u1ba0u1baeu1bafu1bba-u1be5u1c00-u1c23u1c4d-u1c4fu1c5a-u1c7du1ce9-u1cecu1cee-u1cf1u1cf5u1cf6u1d00-u1dbfu1e00-u1f15u1f18-u1f1du1f20-u1f45u1f48-u1f4du1f50-u1f57u1f59u1f5bu1f5du1f5f-u1f7du1f80-u1fb4u1fb6-u1fbcu1fbeu1fc2-u1fc4u1fc6-u1fccu1fd0-u1fd3u1fd6-u1fdbu1fe0-u1fecu1ff2-u1ff4u1ff6-u1ffcu2071u207fu2090-u209cu2102u2107u210a-u2113u2115u2119-u211du2124u2126u2128u212a-u212du212f-u2139u213c-u213fu2145-u2149u214eu2160-u2188u2c00-u2c2eu2c30-u2c5eu2c60-u2ce4u2ceb-u2ceeu2cf2u2cf3u2d00-u2d25u2d27u2d2du2d30-u2d67u2d6fu2d80-u2d96u2da0-u2da6u2da8-u2daeu2db0-u2db6u2db8-u2dbeu2dc0-u2dc6u2dc8-u2dceu2dd0-u2dd6u2dd8-u2ddeu2e2fu3005-u3007u3021-u3029u3031-u3035u3038-u303cu3041-u3096u309d-u309fu30a1-u30fau30fc-u30ffu3105-u312du3131-u318eu31a0-u31bau31f0-u31ffu3400-u4db5u4e00-u9fccua000-ua48cua4d0-ua4fdua500-ua60cua610-ua61fua62aua62bua640-ua66eua67f-ua697ua6a0-ua6efua717-ua71fua722-ua788ua78b-ua78eua790-ua793ua7a0-ua7aaua7f8-ua801ua803-ua805ua807-ua80aua80c-ua822ua840-ua873ua882-ua8b3ua8f2-ua8f7ua8fbua90a-ua925ua930-ua946ua960-ua97cua984-ua9b2ua9cfuaa00-uaa28uaa40-uaa42uaa44-uaa4buaa60-uaa76uaa7auaa80-uaaafuaab1uaab5uaab6uaab9-uaabduaac0uaac2uaadb-uaadduaae0-uaaeauaaf2-uaaf4uab01-uab06uab09-uab0euab11-uab16uab20-uab26uab28-uab2euabc0-uabe2uac00-ud7a3ud7b0-ud7c6ud7cb-ud7fbuf900-ufa6dufa70-ufad9ufb00-ufb06ufb13-ufb17ufb1dufb1f-ufb28ufb2a-ufb36ufb38-ufb3cufb3eufb40ufb41ufb43ufb44ufb46-ufbb1ufbd3-ufd3dufd50-ufd8fufd92-ufdc7ufdf0-ufdfbufe70-ufe74ufe76-ufefcuff21-uff3auff41-uff5auff66-uffbeuffc2-uffc7uffca-uffcfuffd2-uffd7uffda-uffdc][$A-Z_a-zxaaxb5xbaxc0-xd6xd8-xf6xf8-u02c1u02c6-u02d1u02e0-u02e4u02ecu02eeu0370-u0374u0376u0377u037a-u037du0386u0388-u038au038cu038e-u03a1u03a3-u03f5u03f7-u0481u048a-u0527u0531-u0556u0559u0561-u0587u05d0-u05eau05f0-u05f2u0620-u064au066eu066fu0671-u06d3u06d5u06e5u06e6u06eeu06efu06fa-u06fcu06ffu0710u0712-u072fu074d-u07a5u07b1u07ca-u07eau07f4u07f5u07fau0800-u0815u081au0824u0828u0840-u0858u08a0u08a2-u08acu0904-u0939u093du0950u0958-u0961u0971-u0977u0979-u097fu0985-u098cu098fu0990u0993-u09a8u09aa-u09b0u09b2u09b6-u09b9u09bdu09ceu09dcu09ddu09df-u09e1u09f0u09f1u0a05-u0a0au0a0fu0a10u0a13-u0a28u0a2a-u0a30u0a32u0a33u0a35u0a36u0a38u0a39u0a59-u0a5cu0a5eu0a72-u0a74u0a85-u0a8du0a8f-u0a91u0a93-u0aa8u0aaa-u0ab0u0ab2u0ab3u0ab5-u0ab9u0abdu0ad0u0ae0u0ae1u0b05-u0b0cu0b0fu0b10u0b13-u0b28u0b2a-u0b30u0b32u0b33u0b35-u0b39u0b3du0b5cu0b5du0b5f-u0b61u0b71u0b83u0b85-u0b8au0b8e-u0b90u0b92-u0b95u0b99u0b9au0b9cu0b9eu0b9fu0ba3u0ba4u0ba8-u0baau0bae-u0bb9u0bd0u0c05-u0c0cu0c0e-u0c10u0c12-u0c28u0c2a-u0c33u0c35-u0c39u0c3du0c58u0c59u0c60u0c61u0c85-u0c8cu0c8e-u0c90u0c92-u0ca8u0caa-u0cb3u0cb5-u0cb9u0cbdu0cdeu0ce0u0ce1u0cf1u0cf2u0d05-u0d0cu0d0e-u0d10u0d12-u0d3au0d3du0d4eu0d60u0d61u0d7a-u0d7fu0d85-u0d96u0d9a-u0db1u0db3-u0dbbu0dbdu0dc0-u0dc6u0e01-u0e30u0e32u0e33u0e40-u0e46u0e81u0e82u0e84u0e87u0e88u0e8au0e8du0e94-u0e97u0e99-u0e9fu0ea1-u0ea3u0ea5u0ea7u0eaau0eabu0ead-u0eb0u0eb2u0eb3u0ebdu0ec0-u0ec4u0ec6u0edc-u0edfu0f00u0f40-u0f47u0f49-u0f6cu0f88-u0f8cu1000-u102au103fu1050-u1055u105a-u105du1061u1065u1066u106e-u1070u1075-u1081u108eu10a0-u10c5u10c7u10cdu10d0-u10fau10fc-u1248u124a-u124du1250-u1256u1258u125a-u125du1260-u1288u128a-u128du1290-u12b0u12b2-u12b5u12b8-u12beu12c0u12c2-u12c5u12c8-u12d6u12d8-u1310u1312-u1315u1318-u135au1380-u138fu13a0-u13f4u1401-u166cu166f-u167fu1681-u169au16a0-u16eau16ee-u16f0u1700-u170cu170e-u1711u1720-u1731u1740-u1751u1760-u176cu176e-u1770u1780-u17b3u17d7u17dcu1820-u1877u1880-u18a8u18aau18b0-u18f5u1900-u191cu1950-u196du1970-u1974u1980-u19abu19c1-u19c7u1a00-u1a16u1a20-u1a54u1aa7u1b05-u1b33u1b45-u1b4bu1b83-u1ba0u1baeu1bafu1bba-u1be5u1c00-u1c23u1c4d-u1c4fu1c5a-u1c7du1ce9-u1cecu1cee-u1cf1u1cf5u1cf6u1d00-u1dbfu1e00-u1f15u1f18-u1f1du1f20-u1f45u1f48-u1f4du1f50-u1f57u1f59u1f5bu1f5du1f5f-u1f7du1f80-u1fb4u1fb6-u1fbcu1fbeu1fc2-u1fc4u1fc6-u1fccu1fd0-u1fd3u1fd6-u1fdbu1fe0-u1fecu1ff2-u1ff4u1ff6-u1ffcu2071u207fu2090-u209cu2102u2107u210a-u2113u2115u2119-u211du2124u2126u2128u212a-u212du212f-u2139u213c-u213fu2145-u2149u214eu2160-u2188u2c00-u2c2eu2c30-u2c5eu2c60-u2ce4u2ceb-u2ceeu2cf2u2cf3u2d00-u2d25u2d27u2d2du2d30-u2d67u2d6fu2d80-u2d96u2da0-u2da6u2da8-u2daeu2db0-u2db6u2db8-u2dbeu2dc0-u2dc6u2dc8-u2dceu2dd0-u2dd6u2dd8-u2ddeu2e2fu3005-u3007u3021-u3029u3031-u3035u3038-u303cu3041-u3096u309d-u309fu30a1-u30fau30fc-u30ffu3105-u312du3131-u318eu31a0-u31bau31f0-u31ffu3400-u4db5u4e00-u9fccua000-ua48cua4d0-ua4fdua500-ua60cua610-ua61fua62aua62bua640-ua66eua67f-ua697ua6a0-ua6efua717-ua71fua722-ua788ua78b-ua78eua790-ua793ua7a0-ua7aaua7f8-ua801ua803-ua805ua807-ua80aua80c-ua822ua840-ua873ua882-ua8b3ua8f2-ua8f7ua8fbua90a-ua925ua930-ua946ua960-ua97cua984-ua9b2ua9cfuaa00-uaa28uaa40-uaa42uaa44-uaa4buaa60-uaa76uaa7auaa80-uaaafuaab1uaab5uaab6uaab9-uaabduaac0uaac2uaadb-uaadduaae0-uaaeauaaf2-uaaf4uab01-uab06uab09-uab0euab11-uab16uab20-uab26uab28-uab2euabc0-uabe2uac00-ud7a3ud7b0-ud7c6ud7cb-ud7fbuf900-ufa6dufa70-ufad9ufb00-ufb06ufb13-ufb17ufb1dufb1f-ufb28ufb2a-ufb36ufb38-ufb3cufb3eufb40ufb41ufb43ufb44ufb46-ufbb1ufbd3-ufd3dufd50-ufd8fufd92-ufdc7ufdf0-ufdfbufe70-ufe74ufe76-ufefcuff21-uff3auff41-uff5auff66-uffbeuffc2-uffc7uffca-uffcfuffd2-uffd7uffda-uffdc0-9u0300-u036fu0483-u0487u0591-u05bdu05bfu05c1u05c2u05c4u05c5u05c7u0610-u061au064b-u0669u0670u06d6-u06dcu06df-u06e4u06e7u06e8u06ea-u06edu06f0-u06f9u0711u0730-u074au07a6-u07b0u07c0-u07c9u07eb-u07f3u0816-u0819u081b-u0823u0825-u0827u0829-u082du0859-u085bu08e4-u08feu0900-u0903u093a-u093cu093e-u094fu0951-u0957u0962u0963u0966-u096fu0981-u0983u09bcu09be-u09c4u09c7u09c8u09cb-u09cdu09d7u09e2u09e3u09e6-u09efu0a01-u0a03u0a3cu0a3e-u0a42u0a47u0a48u0a4b-u0a4du0a51u0a66-u0a71u0a75u0a81-u0a83u0abcu0abe-u0ac5u0ac7-u0ac9u0acb-u0acdu0ae2u0ae3u0ae6-u0aefu0b01-u0b03u0b3cu0b3e-u0b44u0b47u0b48u0b4b-u0b4du0b56u0b57u0b62u0b63u0b66-u0b6fu0b82u0bbe-u0bc2u0bc6-u0bc8u0bca-u0bcdu0bd7u0be6-u0befu0c01-u0c03u0c3e-u0c44u0c46-u0c48u0c4a-u0c4du0c55u0c56u0c62u0c63u0c66-u0c6fu0c82u0c83u0cbcu0cbe-u0cc4u0cc6-u0cc8u0cca-u0ccdu0cd5u0cd6u0ce2u0ce3u0ce6-u0cefu0d02u0d03u0d3e-u0d44u0d46-u0d48u0d4a-u0d4du0d57u0d62u0d63u0d66-u0d6fu0d82u0d83u0dcau0dcf-u0dd4u0dd6u0dd8-u0ddfu0df2u0df3u0e31u0e34-u0e3au0e47-u0e4eu0e50-u0e59u0eb1u0eb4-u0eb9u0ebbu0ebcu0ec8-u0ecdu0ed0-u0ed9u0f18u0f19u0f20-u0f29u0f35u0f37u0f39u0f3eu0f3fu0f71-u0f84u0f86u0f87u0f8d-u0f97u0f99-u0fbcu0fc6u102b-u103eu1040-u1049u1056-u1059u105e-u1060u1062-u1064u1067-u106du1071-u1074u1082-u108du108f-u109du135d-u135fu1712-u1714u1732-u1734u1752u1753u1772u1773u17b4-u17d3u17ddu17e0-u17e9u180b-u180du1810-u1819u18a9u1920-u192bu1930-u193bu1946-u194fu19b0-u19c0u19c8u19c9u19d0-u19d9u1a17-u1a1bu1a55-u1a5eu1a60-u1a7cu1a7f-u1a89u1a90-u1a99u1b00-u1b04u1b34-u1b44u1b50-u1b59u1b6b-u1b73u1b80-u1b82u1ba1-u1badu1bb0-u1bb9u1be6-u1bf3u1c24-u1c37u1c40-u1c49u1c50-u1c59u1cd0-u1cd2u1cd4-u1ce8u1cedu1cf2-u1cf4u1dc0-u1de6u1dfc-u1dffu200cu200du203fu2040u2054u20d0-u20dcu20e1u20e5-u20f0u2cef-u2cf1u2d7fu2de0-u2dffu302a-u302fu3099u309aua620-ua629ua66fua674-ua67dua69fua6f0ua6f1ua802ua806ua80bua823-ua827ua880ua881ua8b4-ua8c4ua8d0-ua8d9ua8e0-ua8f1ua900-ua909ua926-ua92dua947-ua953ua980-ua983ua9b3-ua9c0ua9d0-ua9d9uaa29-uaa36uaa43uaa4cuaa4duaa50-uaa59uaa7buaab0uaab2-uaab4uaab7uaab8uaabeuaabfuaac1uaaeb-uaaefuaaf5uaaf6uabe3-uabeauabecuabeduabf0-uabf9ufb1eufe00-ufe0fufe20-ufe26ufe33ufe34ufe4d-ufe4fuff10-uff19uff3f]*$/
From the ECMAScript specification in section 7.6 Identifier Names and Identifiers, a valid identifier is defined as:
Identifier ::
IdentifierName but not ReservedWord
IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
UnicodeEscapeSequence
UnicodeLetter
any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”,
“Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.
UnicodeCombiningMark
any character in the Unicode categories “Non-spacing mark (Mn)” or “Combining spacing mark (Mc)”
UnicodeDigit
any character in the Unicode category “Decimal number (Nd)”
UnicodeConnectorPunctuation
any character in the Unicode category “Connector punctuation (Pc)”
UnicodeEscapeSequence
see 7.8.4.
HexDigit :: one of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
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, cause it should.
var ᾩ = "something";
var ĦĔĽĻŎ = "hello";
var 〱〱〱〱 = "less than? wtf";
var जावास्क्रिप्ट = "javascript"; // ok that's JavaScript in hindi
var KingGeorgeⅦ = "Roman numerals, awesome!";
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.
链接地址: http://www.djcxy.com/p/12380.html上一篇: 什么是VanillaJS?