ES6具有多个变量类型的解构分配 [英] ES6 destructuring assignment with more than one variable type

查看:83
本文介绍了ES6具有多个变量类型的解构分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回5个对象的函数,我想使用const声明其中4个对象,并使用let声明其中1个对象.如果我想要使用const声明的所有对象,我可以这样做:

I have a function that returns 5 objects, and I would like to declare 4 of them using const and 1 of them using let. If I wanted all objects declared using const I could do:

const { thing1, thing2, thing3, thing4, thing5 } = yield getResults();

我当前的解决方法是:

const results = yield getResults();

const thing1 = results.thing1;
const thing2 = results.thing2;
const thing3 = results.thing3;
const thing4 = results.thing4;

let thing5 = results.thing5; 

但我想知道解构分配是否可以让您更优雅地完成此任务.

But I'm wondering if destructuring assignment allows you to do this more elegantly.

MDN 或stackoverflow上没有提及此问题,据我所见.

No mention of this question on MDN or on stackoverflow, as far as I can see.

推荐答案

无法执行同时初始化letconst变量的解构.但是,可以将对const的赋值减少为另一个分解:

It isn't possible to perform a destructure that initialises both let and const variables simultaneously. However the assignments to const can be reduced to another destructure:

const results = yield getResults()

const { thing1, thing2, thing3, thing4 } = results

let thing5 = results.thing5

这篇关于ES6具有多个变量类型的解构分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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