最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why Google Chrome console throws "SyntaxError: Unexpected token }" when inputted ( - Stack Overfl

programmeradmin9浏览0评论

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)?

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)?

Share Improve this question edited Apr 9, 2011 at 4:41 itchyny asked Apr 9, 2011 at 4:22 itchynyitchyny 1291 gold badge1 silver badge3 bronze badges 3
  • Care to post some of the code that is throwing said error? – Seth Commented Apr 9, 2011 at 4:36
  • 1 @Seth: He did, it's just not easy to see. – BoltClock Commented Apr 9, 2011 at 4:38
  • Related: stackoverflow./questions/11989226/… – Álvaro González Commented Dec 20, 2012 at 13:21
Add a ment  | 

1 Answer 1

Reset to default 22

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 mand-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._mandLineAPI) || {}) {
    <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._mandLineAPI) || {}) {
}
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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论