干净的方法可以同时保留原始变量和销毁 [英] Clean way to keep original variable and destruction at the same time

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

问题描述

是否有一种更干净的方法(使用至少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),而我真的希望它也可以是const.

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天全站免登陆