admin管理员组文章数量:1221309
I am surprised that the following code when input into the Chrome js console:
{} instanceof Object
results in this error message:
Uncaught SyntaxError: Unexpected token instanceof
Can anyone please tell me why that is and how to fix it?
I am surprised that the following code when input into the Chrome js console:
{} instanceof Object
results in this error message:
Uncaught SyntaxError: Unexpected token instanceof
Can anyone please tell me why that is and how to fix it?
Share Improve this question asked Jan 13, 2015 at 11:05 balteobalteo 24.7k67 gold badges234 silver badges436 bronze badges4 Answers
Reset to default 15The grammar for instanceof is:
RelationalExpression instanceof ShiftExpression
per ECMA-262 §11.8.
The punctuator {
at the start of a statement is seen as the start of a block, so the following }
closes the block and ends the statement.
The following instanceof
operator is the start of the next statement, but it can't be at the start because it must be preceded by a RelationalExpression, so the parser gets a surprise.
You need to force {}
to be seen as an object literal by putting something else at the start of the statement, e.g.
({}) instanceof Object
{}
, in that context, is a block, not an object literal.
You need change the context (e.g. by wrapping it in (
and )
) to make it an object literal.
({}) instanceof Object;
If you try this:
var a = {}
a instanceof Object
outputs true
, which is the expected output.
However, in your case
{} instanceof Object
The above doesn't outputs true.
The latter isn't the same as the first one. In the first case, we create an object literal, while in the second case we don't. Hence you get this error.
Try
var p = {}
p instanceof Object
本文标签: Uncaught SyntaxError Unexpected token instanceof (with Chrome Javascript console)Stack Overflow
版权声明:本文标题:Uncaught SyntaxError: Unexpected token instanceof (with Chrome Javascript console) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739269293a2155742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论