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
131 views
in Technique[技术] by (71.8m points)

javascript - 为什么2 + 40等于42?(Why does 2+?40 equal 42?)

I was baffled when a colleague showed me this line of JavaScript alerting 42.(当一位同事向我展示这一行警告42时,我感到很困惑。)

 alert(2+ 40); 

It quickly turns out that what looks like a minus sign is actually an arcane Unicode character with clearly different semantics.(事实证明,看起来像减号的实际上是一个神秘的Unicode字符,具有明显不同的语义。)

This left me wondering why that character doesn't produce a syntax error when the expression is parsed.(这让我想知道为什么该字符在解析表达式时不会产生语法错误。)

I'd also like to know if there are more characters behaving like this.(我还想知道是否有更多的人物表现得像这样。)   ask by GOTO 0 translate from so

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

1 Reply

0 votes
by (71.8m points)

That character is "OGHAM SPACE MARK" , which is a space character.(那个角色是“OGHAM SPACE MARK” ,这是一个空间角色。)

So the code is equivalent to alert(2+ 40) .(所以代码相当于alert(2+ 40) 。)

I'd also like to know if there are more characters behaving like this.(我还想知道是否有更多的人物表现得像这样。)

Any Unicode character in the Zs class is a white space character in JavaScript , but there don't seem to be that many .(Zs类中的任何Unicode字符都是JavaScript中的空白字符但似乎没有那么多 。)

However, JavaScript also allows Unicode characters in identifiers , which lets you use interesting variable names like ?_? .(但是, JavaScript还允许在标识符中使用Unicode字符 ,这使您可以使用有趣的变量名称,如?_? 。)


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

...