保持原始变量和同时解构的干净方法 [英] Clean way to keep original variable and destructure at the same time

查看:23
本文介绍了保持原始变量和同时解构的干净方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更简洁的方法来做到这一点(至少是 ES 草案并有 babel 插件,例如 ES6、ES7 等):

Is there a cleaner way to do this (with anything that is at least an ES draft and has a babel plugin, i.e., ES6, ES7, etc.):

const { a, b } = result = doSomething();

我想将整体结果保留为一个单一对象,但同时也对其进行解构.它在技术上是有效的,但是 result 是隐式声明的(带有隐式 var),而我真的希望它也是一个常量.

Where I want to keep the overall result as one singular object, but also destructure it at the same time. It technically works, but result is implicitly declared (with an implicit var), while I'd really like it to also be a const.

我目前正在这样做:

const result = doSomething();
const { a, b } = result;

这又奏效了,但有点冗长,因为我需要重复这个模式几十次.

Which again works, but it's slightly on the verbose side, since I need to repeat this pattern dozens of times.

理想情况下,我想要一些类似的东西:

I'd ideally want something along the lines of:

const { a, b } = const result = doSomething();

但这显然是一种无效的语法.

But that is obviously an invalid syntax.

推荐答案

一种可能的方式:

const result = doSomething(), 
    { a, b } = result;

不过,您仍然需要重复该名称.const 令牌不太合适.)

You still have to duplicate the name, though. const token isn't quite right-handy. )

这篇关于保持原始变量和同时解构的干净方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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