Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

naming conventions - Why aren't ◎?◎ and ? valid JavaScript variable names?

I noticed that in Internet Explorer (but, unfortunately, not in the other browsers I tested), you can use some Unicode variable names. This made my day, and I was absolutely delighted that I could write fun Unicode-laden code like this:

var ктоε?ναι草泥马 = "You dirty horse.",
    happy?n?mat?p?eia = ":)Yay!",
    ?_? = "emoticon";

alert(ктоε?ναι草泥马 + happy?n?mat?p?eia + ?_?);

For some reason, though, ◎?◎, ?_? and ? are not valid variable names.

Why do ?_? and 草泥马 work, but ◎?◎, ?_? and ? don't?

EDIT: Test it out in your browser on JSFiddle. I've tested it in Internet Explorer 9, Chrome, Firefox, and Opera. So far, it seems to only work in Internet Explorer 9. (I don't know about Internet Explorer 8 and below.) Let me know if it works in another browser.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

?_? 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 you if any string that you enter is a valid JavaScript variable name according to ECMAScript 5.1 and Unicode 6.1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...