Why does “{} == {}” result in a SyntaxError? [duplicate]

末鹿安然 提交于 2019-12-10 20:18:51

问题


Possible Duplicate:
Why {} != ( {} ) in JavaScript?

I tried it today and it threw me SyntaxError today and it made me wonder; what's wrong with this?

{} === {}
{} == {}

What's wrong?


回答1:


When { is the first token in a line, it's considered the start of block.

{
  some();
  statements();
  here();
}

And not an object literal. A block of code cannot be equal to anything, it's not an assignable thing.

({}) === {}

That will set the parser straight.




回答2:


Use parens. Parentheses turn the ambiguous code into an expression:

({}) === ({})

Or:

({} === {})


来源:https://stackoverflow.com/questions/14532355/why-does-result-in-a-syntaxerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!