为什么Google Chrome控制台会抛出“SyntaxError:Unexpected token}”当输入时( [英] Why Google Chrome console throws "SyntaxError: Unexpected token }" when inputted (

查看:1048
本文介绍了为什么Google Chrome控制台会抛出“SyntaxError:Unexpected token}”当输入时(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Chrome的控制台中,当我们输入

 

和Enter,Chrome说SyntaxError:Unexpected token}为什么?输入只是(,包括没有}。



当我们输入

  console.log(

没有}!!!



下一个标记应该是参数列表或),所以错误消息应该是预期的参数列表或未关闭(或什么。



我想知道,是控制台输入解析为 StatementList(opt)(在ECMA-262中定义)?

解决方案

编辑:我找到了得到评估的确切代码,代码位于src / third_party / WebKit / Source / WebCore / inspector / InjectedScriptSource.js中。



在Chrome控制台评估您的代码之前,它会使用块将其封装到中,以将命令行功能纳入范围。实际上是在大括号内进行评估。这个意外的}令牌是Chrome自动发布的令牌。



Chrome传递给 eval ((window& window.console&& window.console._commandLineAPI)|| {b
$ b

  ){
<您的代码在这里>
};

因为它是一个简单的文本替换,所以下面的例子可以工作,结果是一个可以扩展的对象查看答案属性:

 } 0,{answer:42 

其中(重新格式化)等同于:

< ((window& window.console&& window.console._commandLineAPI)|| {}){
}
0,{pre> 答案:42};

开始时的} code>与块。 0,部分对于强制对象文本被解析为表达式而不是另一个块是必需的。然后, {answer:42 是被插入的} 标记关闭的对象字面值的开始。 / p>

为获得更多乐趣,以下是其他一些可用的输入(及其结果):

 > } {//空块,所以没有值
undefined

> }!{//!{} === false
false

> } !! {// !! {} === true
true

> } + {valueOf:function(){return 123; }
123


In Google Chrome's console, when we input

(

and Enter, Chrome says "SyntaxError: Unexpected token }" Why? Input is just "(", including no "}".

We get the same error when we input

console.log(

There's no "}"!!!

Next token should be arguments list or ")" so error message should be "Expected arguments list" or "Unclosed (" or something.

And I wanna know, is console input parsed as StatementList(opt) (defined in ECMA-262)?

解决方案

Edit: I found the exact code that gets evaluated. The code is in "src/third_party/WebKit/Source/WebCore/inspector/InjectedScriptSource.js".

Before the Chrome console evaluates your code, it wraps it in a with block to bring the command-line functions into scope. So what you type is actually evaluated inside braces. The unexpected "}" token is the one put in automatically by Chrome.

The code that Chrome passes to eval is

with ((window && window.console && window.console._commandLineAPI) || {}) {
    <your code here>
};

Because it's a simple text substitution, the following example works and the result is an object which you can expand to see the answer property:

} 0, { answer: 42

Which (reformatted) is equivalent to:

with ((window && window.console && window.console._commandLineAPI) || {}) {
}
0, { answer: 42 };

The } at the beginning closes the with block. The 0, part is necessary to force the object literal to be parsed as an expression instead of another block. Then, the { answer: 42 is the beginning of an object literal that gets closed by the inserted } token.

For more fun, here are some other inputs that work (and their results):

> }{ // an empty block, so no value
  undefined

> }!{ // !{} === false
  false

> }!!{ // !!{} === true
  true

> } +{ valueOf: function() { return 123; }
  123

这篇关于为什么Google Chrome控制台会抛出“SyntaxError:Unexpected token}”当输入时(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆