What characters are valid for JavaScript variable names?

后端 未结 12 1870
情深已故
情深已故 2020-11-21 04:26

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

相关标签:
12条回答
  • 2020-11-21 04:50

    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):

    • Uppercase letter (Lu)
    • Lowercase letter (Ll)
    • Titlecase letter (Lt)
    • Modifier letter (Lm)
    • Other letter (Lo)
    • Letter number (Nl)

    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!";
    
    0 讨论(0)
  • 2020-11-21 04:50

    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.

        function isValidVarName( name ) {
        try {
            return name.indexOf('}') === -1 && eval('(function() { a = {' + name + ':1}; a.' + name + '; var ' + name + '; }); true');
        } catch( e ) {
            return false;
        }
        return true;
    }
    
    // so we can see the test code
    var _eval = eval;
    window.eval = function(s) {
        console.log(s);
        return _eval(s);
    }
    
    console.log(isValidVarName('name'));
    console.log(isValidVarName('$name'));
    console.log(isValidVarName('not a name'));
    console.log(isValidVarName('a:2,b'));
    console.log(isValidVarName('"a string"'));
    
    console.log(isValidVarName('xss = alert("I\'m in your vars executin mah scrip\'s");;;;;'));
    console.log(isValidVarName('_;;;'));
    console.log(isValidVarName('_=location="#!?"'));
    
    console.log(isValidVarName('ᾩ'));
    console.log(isValidVarName('ĦĔĽĻŎ'));
    console.log(isValidVarName('〱〱〱〱'));
    console.log(isValidVarName('जावास्क्रिप्ट'));
    console.log(isValidVarName('KingGeorgeⅦ'));
    console.log(isValidVarName('}; }); alert("I\'m in your vars executin\' mah scripts"); true; // yeah, super valid'));
    console.log(isValidVarName('if'));
    
    0 讨论(0)
  • 2020-11-21 04:52

    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 use jQuery. To do this, simply set:

    jQuery.noConflict();
    

    This page explains how to do this.

    0 讨论(0)
  • 2020-11-21 04:54

    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:

    • Letters: Lu, Ll, Lt, Lm, Lo, Nl
      (combined in the regex above as “L”)
    • Combining marks (“modifiers”): Mn, Mc
    • Digits: Nd
    • Connectors: Pc

    *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.)

    0 讨论(0)
  • 2020-11-21 04:54

    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 catagories, Lo, Ll, Lu, Lt, Lm and Nl. And when you look up those catagories you will see that this opens up a lot more possibilities than just latin letters. Just search for "unicode catagories" in google and you can find them.

    0 讨论(0)
  • 2020-11-21 04:54

    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.

    ^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*+$
    

    In addition, the name cannot be one of the following reserved words.

    break, do, instanceof, typeof, case, else, new, var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try, class, enum, extends, super, const, export, import, implements, let, private, public, yield, interface, package, protected, static, null, true, false

    0 讨论(0)
提交回复
热议问题