javascript tutorial - [Solved-4 Solutions] Smile javascript - javascript - java script - javascript array



Problem:

We noticed that in Internet Explorer (but, unfortunately, not in the other browsers we tested), we can use some Unicode variable names.

var ктоείναι開心馬 = "We happy horse.",
    happy☺n☺mat☺p☺eia = ":)Yay!",
    ಠ_ಠ = "emoticon";

alert(ктоείναι開心馬 + happy☺n☺mat☺p☺eia + ಠ_ಠ);
click below button to copy the code. By JavaScript tutorial team

For some reason, though, ◎ܫ◎, ♨_♨ and ☺ are not valid variable names. Why do ಠ_ಠ and 開心馬 work, but ◎ܫ◎, ♨_♨ and ☺ don't? I've tested it in Internet Explorer 9, Chrome, Firefox, and Opera. So far, it seems to only work in Internet Explorer 9. (We don't know about Internet Explorer 8 and below.) Let me know if it works in another browser.

Solution 1:

ಠ_ಠ and 草泥马 only contain "letters" used in actual alphabets; that is, ಠ is a symbol from the Kannada alphabet, and 草泥马 consists of Chinese characters. ◎ and ☺, however, are purely symbols; they are not associated with any alphabet. The ECMAScript standard, chapter 7.6 (which all the browsers except Internet Explorer are following), states that an identifier must start with one of the following.

  • a Unicode letter
  • $ or _
  • \ followed by a unicode escape sequence.

The following characters of an identifier must be one of the following.

  • any of the characters permitted at the start
  • a Unicode combining mark
  • a Unicode digit
  • a Unicode connector punctuation
  • a zero-width-non-joiner
  • a zero-width joiner

IE goes beyond the standard and is permissive enough to allow some symbols, such as ☺.

There’s a tool that will tell we if any string that we enter is a valid JavaScript variable name according to ECMAScript 5.1 and Unicode 6.1.

Solution 2:

EcmaScript 262, section 7.6 says names must start with $, _, or a Unicode letter, and after that may contain either those characters, or Unicode combining marks, Unicode digits, or Unicode connector punctuation (and a couple of format-control characters that are language specific.) So, the difference between allowed and not allowed identifiers in our cases is probably whether the leading character is considered a "letter"

Solution 3:

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-joinercharacters, 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 we if any string that we enter is a valid JavaScript variable name according to ECMAScript 5.1 and Unicode 6.3:

Learn javascript - javascript tutorial - permalink - javascript examples - javascript programs

P.S. If we were to summarize all these rules in a single ASCII-only regular expression for JavaScript, it would be over 9,000 characters long. Here it is:

// ECMAScript 5.1 / Unicode v6.3.0:
/^(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|null|this|true|void|with|break|catc
click below button to copy the code. By JavaScript tutorial team

Solution 4:

For the great justice: since most things in JS are "objects", we can use square-bracket syntax:

window["◎ܫ◎"] = true;
alert(window["◎ܫ◎"]); // alerts "true"

click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Smile javascript