the*_*lla 5 javascript ecmascript-6
我有一个函数返回5个对象,我想声明其中4个使用const,其中1个使用let.如果我想要使用const我声明的所有对象可以:
const { thing1, thing2, thing3, thing4, thing5 } = yield getResults();
Run Code Online (Sandbox Code Playgroud)
我目前的解决方法是:
const results = yield getResults();
const thing1 = results.thing1;
const thing2 = results.thing2;
const thing3 = results.thing3;
const thing4 = results.thing4;
let thing5 = results.thing5;
Run Code Online (Sandbox Code Playgroud)
但我想知道解构分配是否允许你更优雅地做到这一点.
就我所知,在MDN或stackoverflow 上没有提到这个问题.
不可能执行同时初始化let和const变量的结构.但是,分配const可以减少到另一个结构:
const results = yield getResults()
const { thing1, thing2, thing3, thing4 } = results
let thing5 = results.thing5
Run Code Online (Sandbox Code Playgroud)
您仍然可以单独使用解构:
const results = yield getResults();
const { thing1, thing2, thing3, thing4} = results;
let { thing5 } = results;
Run Code Online (Sandbox Code Playgroud)
或者,可以这样做
let thing5;
const { thing1, thing2, thing3, thing4 } = { thing5 } = yield getResults();
Run Code Online (Sandbox Code Playgroud)
但我想应该避免减少你的代码的WTF /分钟.
| 归档时间: |
|
| 查看次数: |
553 次 |
| 最近记录: |