我发现当我转义从 0 到 7 开始的数字并提醒它们时,我得到一个奇怪的符号:
alert( "\0" ); // or \1, \2, \3...\7
这仅在我发出警报时才可见,但 document.write 和 console.log 不会显示它。转义大于 7 的数字看起来没问题。我正在使用最新版本的 Chrome。为什么我会遇到这个奇怪的 Angular 色?谢谢。
最佳答案
section 7.8.4 中描述了一般如何解析字符串文字。
但是,您看到的行为在 Annex B.1.2 中进行了描述。本节介绍旧 ES 版本中的八进制转义序列,该序列似乎仍然受支持。结果字符定义如下:
- The CV of OctalEscapeSequence :: OctalDigit [lookahead ∉ DecimalDigit] is the character whose code unit value is the MV of the OctalDigit.
- The CV of OctalEscapeSequence :: ZeroToThree OctalDigit [lookahead ∉ DecimalDigit] is the character whose code unit value is (8 times the MV of the ZeroToThree) plus the MV of the OctalDigit.
- The CV of OctalEscapeSequence :: FourToSeven OctalDigit is the character whose code unit value is (8 times the MV of the FourToSeven) plus the MV of the OctalDigit.
- The CV of OctalEscapeSequence :: ZeroToThree OctalDigit OctalDigit is the character whose code unit value is (64 (that is, 82) times the MV of the ZeroToThree) plus (8 times the MV of the first OctalDigit) plus the MV of the second OctalDigit.
\x
(\xx
, \xxx
) 仅当 x
是一个时才被解释为八进制序列八进制数字,即 0
和 7
之间。
所以,所有字符 \0
- \7
实际上都是 control characters 。较高的值指的是其他字符,例如 \101
是 A
。
我无法告诉您为什么 alert
显示奇怪的字符,而控制台没有显示任何内容......这可能是一个实现细节。
有趣的事实:严格模式下不允许八进制转义序列。
有趣的事实#2: \0
实际上不是八进制转义序列,并且仍然可以在严格模式下工作,因为它有自己的生产规则(请参阅 section 7.8.4 ) 。 OTHA,\00
是一个八进制序列,在严格模式下会抛出错误。
关于javascript - 提醒转义号码时出现奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791256/