这是ASI的问题还是仅仅是返回语法? [英] Is this a problem with ASI or just the return grammar?

查看:91
本文介绍了这是ASI的问题还是仅仅是返回语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们经常谈论关于JavaScript令人讨厌的ASI(自动分号插入),并经常引用此示例 ...

People often talk about JavaScript's nasty ASI (Automatic Semicolon Insertion), and often cite this example...

function a() {
    return [
        'a',
        'b',
        'c'
    ];
}

function b() {
    return
    [
        'a',
        'b',
        'c'
    ];
}

console.log(a(), b());
// ["a", "b", "c"] undefined

是在 b <)中的返回之后插入了分号,或者JavaScript语法是否表明返回值必须在它之后明确说明(即没有 \ n )?

Is there really a semicolon being inserted after return in b(), or does the JavaScript grammar state that the return value must explicitly be stated after it (i.e. no \n)?

推荐答案

我认为没有插入分号。只是那里有一个解析模糊,解决方案是将返回和下面的表达式视为两个单独的语句。我理解为什么它从纯粹的语法角度来看是有意义的,但是在返回语句的特定情况下它似乎是一个奇怪的决定,因为决定保证离开一个永不执行的孤儿表达式声明悬在它之后。

I don't think there's a semicolon "inserted". It's just that there's a parse ambiguity there, and the resolution is to treat the "return" and the following expression as two separate statements. I understand why it makes sense from a purely grammatical standpoint, but it seems like a weird decision in the specific case of the "return" statement as the decision is guaranteed to leave a never-executed orphan expression statement dangling after it.

我想知道这个技巧产生了多少个SO转发点?

I wonder how many aggregate SO rep points that trick has generated?

编辑2014年4月4日—从技术上讲,将语句拆分为两个语句的决定是,称为分号插入。实际上没有人看到插入的分号;它是解析器想象力的一个虚构:)无论你怎么称呼它的效果是一样的。

edit 04 Apr 2014 — technically that decision to split the statement into two statements is called "semicolon insertion". Nobody actually sees the inserted semicolon; it's a figment of the parser's imagination :) The effect is the same whatever you call it.

这篇关于这是ASI的问题还是仅仅是返回语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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