当一位同事向我展示这行 JavaScript 警报 42 时,我感到很困惑。
alert(2+ 40);
很快发现,看起来像减号的东西实际上是一个神秘的 Unicode 字符,具有明显不同的语义。
这让我想知道为什么在解析表达式时该字符不会产生语法错误。我也想知道是否还有更多这样的 Angular 色。
最佳答案
那个字符是"OGHAM SPACE MARK" ,这是一个空格字符。所以代码相当于alert(2+ 40)
。
I'd also like to know if there are more characters behaving like this.
Zs 类中的任何 Unicode 字符 is a white space character in JavaScript , but there don't seem to be that many .
但是,JavaScript also allows Unicode characters in identifiers ,它允许您使用有趣的变量名称,例如 ಠ_ಠ
。
关于javascript - 为什么 2+ 40 等于 42?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507143/