为什么不是◎ܫ◎和☺有效的JavaScript变量名? [英] Why aren't ◎ܫ◎ and ☺ valid JavaScript variable names?

查看:181
本文介绍了为什么不是◎ܫ◎和☺有效的JavaScript变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在Internet Explorer中(但不幸的是,在我测试的其他浏览器中没有),您可以使用一些Unicode变量名。这让我很高兴,我非常高兴我可以写出这样有趣的Unicode代码:

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?

编辑:在 JSFiddle 上的浏览器中测试一下。我已经在互联网 资源管理器  9,Chrome,Firefox和Opera中进行了测试。到目前为止,它似乎只能在Internet Explorer  9中工作。 (我不知道互联网 资源管理器  8及以下。)让我知道它是否适用于其他浏览器。

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.

推荐答案

ಠ_ಠ
和草泥马只包含实际字母表中使用的字母;也就是说,ಠ
是来自卡纳达语字母表的符号,草泥马由中文字符组成。

ಠ_ಠ and 草泥马 only contain "letters" used in actual alphabets; that is, ಠ is a symbol from the Kannada alphabet, and 草泥马 consists of Chinese characters.

◎然而,are纯粹是符号;它们与任何字母表无关。

◎ and ☺, however, are purely symbols; they are not associated with any alphabet.

ECMAScript标准,第7.6章(除了Internet Explorer之外的所有浏览器都遵循),声明标识符必须以下列之一开头。

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.


  • Unicode字母

  • $ _

  • \ 后跟一个unicode转义序列。

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

标识符的后续字符必须是以下之一。

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


  • 开头允许的任何字符

  • Unicode组合标记

  • 一个Unicode数字

  • 一个Unicode连接符标点符号

  • 零 - width-non-joiner

  • 零宽度木匠

  • 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超出标准范围很容易允许一些符号,例如☺。

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

工具 t根据ECMAScript 5.1和Unicode 6.1,hat会告诉您输入的任何字符串是否是有效的JavaScript变量名。

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.

这篇关于为什么不是◎ܫ◎和☺有效的JavaScript变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆