如果没有大括号,让进来 [英] let in if without braces

查看:25
本文介绍了如果没有大括号,让进来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (true) {
  let x = 5
}

按预期工作(没有语法错误),但是

works as expected (no syntax error), but

if (true) let x = 5

在 Node 4.1.0 和 babel 中抛出 SyntaxError: Unexpected strict mode reserved word

throws SyntaxError: Unexpected strict mode reserved word in Node 4.1.0 and babel

这是预期的行为吗?我知道这是一个愚蠢的例子.我只是想知道这是否是一个错误.

Is this expected behavior? I know that this is a stupid example. I'm just wondering wether this is a bug or not.

推荐答案

是的,这是预期的行为.if 语句的产生式规则

Yes it is expected behavior. The production rule of an if statement is

 if ( Expression[In, ?Yield] ) Statement[?Yield, ?Return]

但是一个 let 声明 不是 Statement,因此不允许出现在这个位置:

but a let declaration is not a Statement and is therefore not allowed in this position:

Statement[Yield, Return] :
    BlockStatement[?Yield, ?Return]
    VariableStatement[?Yield]
    EmptyStatement
    ExpressionStatement[?Yield]
    IfStatement[?Yield, ?Return]
    BreakableStatement[?Yield, ?Return]
    ContinueStatement[?Yield]
    BreakStatement[?Yield]
    [+Return] ReturnStatement[?Yield]
    WithStatement[?Yield, ?Return]
    LabelledStatement[?Yield, ?Return]
    ThrowStatement[?Yield]
    TryStatement[?Yield, ?Return]
    DebuggerStatement

Declaration[Yield] :
    HoistableDeclaration[?Yield]
    ClassDeclaration[?Yield]
    LexicalDeclaration[In, ?Yield]

LexicalDeclaration[In, Yield] :
    LetOrConst BindingList[?In, ?Yield] ;

这篇关于如果没有大括号,让进来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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