什么时候JS将{}解释为空块而不是空对象? [英] When does JS interpret {} as an empty block instead of an empty object?

查看:115
本文介绍了什么时候JS将{}解释为空块而不是空对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读:

 声明:
阻止
VariableStatement
EmptyStatement
ExpressionStatement
......很多其他东西......

这是一种非常奇特的方式,可以说语句可以是块,变量语句,空语句,表达式语句,或许多其他的东西。请注意,第一个选项是阻止:

 阻止:
{StatementList(opt)}

StatementList:
语句
StatementList语句

这是再一次,一种奇特的方式,说一个块是 {,可选地后跟一堆语句,然后是}



这就是你在你的例子中看到的:在JavaScript解析器认为你拥有的东西之前可能是一个对象文字(在<$ c下的某个地方定义) $ c> ExpressionStatement ,'声明'可能是第四件事),它首先认为你有一个'阻止'。



编辑:如果你愿意,你可以在JavaScript引擎的源代码中看到它:




  • 在V8,Chrome的JavaScript引擎中,我们进入< a href =https://github.com/v8/v8/blob/3969195bb2e224db3bf60d3a1e9bf17ee1be88b8/src/parser.cc#L1572 =nofollow noreferrer> Parser :: Parse声明它首先检查是我们是否'依赖于 {,如果是,则解析为阻止。

  • 在SpiderMonkey中,Firefox的JavaScript引擎,我们来自< a href =http://hg.mozilla.org/mozilla-central/file/78a4540b0a9c/js/src/frontend/Parser.cpp#l5413\"rel =nofollow noreferrer> Parser ::语句 再次查看第一次检查是针对 {并将其解析为块语句。



关于你的第二个问题,关于这个问题已经详细介绍了这个问题。 。总结一句话:Node.js将您的输入视为表达式(因此它不能是阻止),而Firebug / Chrome开发工具将其视为语句。


I was reading the answer to this question (about the "wat" video) and it said:

  1. {}+[]
    This is interpreted as an empty block of code, unary plus and empty array. First part does nothing, array is converted to a comma-separated string of it's elements (empty string for empty array), then to a number (empty string is converted to 0), hence 0.

I am currently learning JS from "The Definitive Guide" so I try to really understand things like that.

My question is, when does JS decide to interpret {} as an empty block of code, instead of an empty object?

Also, there are some inconsistencies between Node.js and Firebug which I would like to understand.

Firebug:

Node.js:

解决方案

Let's look at the language grammar, shall we? Section 12, Statements:

Statement :
    Block
    VariableStatement
    EmptyStatement
    ExpressionStatement
    ...lots of other stuff...

That's a very fancy way of saying that a statement can be a block, a variable statement, an empty statement, an expression statement, or lots of other stuff. Notice that the first option there is a 'Block':

Block :
    { StatementList(opt) }

StatementList :
    Statement
    StatementList Statement

Which is again, a fancy way of saying that a block is a {, optionally followed by a bunch of statements, followed by a }.

And that's what you see in your example: Before the JavaScript parser thinks that what you have could be an object literal (which is defined somewhere under ExpressionStatement, the 4th thing a 'Statement' could be), it first thinks that you have a 'Block'.

Edit: If you want, you can see it live in a JavaScript engine's source code:

Regarding your second question, that's been covered to great detail on this question. To summarise in a sentence: Node.js treats your input as if it were an expression (thus it can't be a 'Block'), while Firebug/Chrome dev tools treat it like a 'Statement'.

这篇关于什么时候JS将{}解释为空块而不是空对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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