在return语句之后具有多个值的意义是什么?

nic*_*oum 1 javascript return

我在搞乱AST树解析器,发现一个ReturnStatement可以有多个expressions。如下面的代码片段所示,return语句后有多个值,但代码已编译并成功运行(它返回的最后一个值)。

function test() {
  return 1, 2, 3;
}

console.log(test());
Run Code Online (Sandbox Code Playgroud)

AST形式:

{
    "type": "ReturnStatement",
    "start": 13,
    "end": 24,
    "argument": {
        "type": "SequenceExpression",
        "start": 20,
        "end": 23,
        "expressions": [{
            "type": "Literal",
            "start": 27,
            "end": 28,
            "value": 1,
            "raw": "1"
        }, {
            "type": "Literal",
            "start": 30,
            "end": 31,
            "value": 2,
            "raw": "2"
        }, {
            "type": "Literal",
            "start": 33,
            "end": 34,
            "value": 2,
            "raw": "3"
        }]
    }
}
Run Code Online (Sandbox Code Playgroud)

此功能和/或错误的意义是什么?
您什么时候要使用此语法?

geo*_*org 5

1, 2, 3不是多个表达式,它是一个带有逗号运算符的表达式,SequenceExpression在AST中被调用。仅当子表达式有副作用时,逗号才有意义。例如,有些人喜欢编写这样的reduce回调:

let count = ary => ary.reduce(function (o, x) { 
    return o[x] = ++o[x] || 1, o 
}, {})
Run Code Online (Sandbox Code Playgroud)

在这里,逗号用于执行副作用o[x] = ...,然后返回累加器。

逗号运算符主要是为了简洁起见,如果没有它,您总是可以相处的:

let count = ary => ary.reduce(function (o, x) { 
    o[x] = ++o[x] || 1; 
    return o 
}, {})
Run Code Online (Sandbox Code Playgroud)