在JavaScript语法Jn传播后加上分号会中断执行,并显示错误"Unexpected token =". [英] Having semicolon after spread syntax jn javascript breaks execution with error "Unexpected token ="

查看:46
本文介绍了在JavaScript语法Jn传播后加上分号会中断执行,并显示错误"Unexpected token =".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么吗

Can someone explain me why

const getabc = ()=> ({a:'aa',b:'bb',c:123});
let a, b, c;
{ a, b, c } = {...getabc()}

这有效

const getabc = ()=> ({a:'aa',b:'bb',c:123});
let a, b, c;
{ a, b, c } = {...getabc()};

这不是(请注意分号结尾)

this does not (note semicolon at the end)

推荐答案

这与扩展语法或分号无关.

This has nothing to do with spread syntax or semicolons.

没有像varconstlet之类的对象进行对象分解的分配必须使用括号(或以其他方式在包含它的较大语句中以表达式的形式出现),因为否则JS将将开头括号解析为一个块的开头:

Object destructuring assignments that are not preceded with something like var, const, or let must use parentheses (or in some other way occur as a an expression within a larger statement containing it) because otherwise JS will parse the opening brace as the beginning of a block:

const getabc = ()=>({a:'aa',b:'bb',c:123});
let a, b, c;
({ a, b, c } = {...getabc()});

同时,这里没有使用扩展语法的意义,因此您可以删除它:

At the same time, there is no point in using spread syntax here, so you can remove that:

const getabc = ()=>({a:'aa',b:'bb',c:123});
let a, b, c;
({ a, b, c } = getabc());

这篇关于在JavaScript语法Jn传播后加上分号会中断执行,并显示错误"Unexpected token =".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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