admin管理员组文章数量:1345707
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();
My current workaround is:
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.
No mention of this question on MDN or on stackoverflow, as far as I can see.
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();
My current workaround is:
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.
No mention of this question on MDN or on stackoverflow, as far as I can see.
Share Improve this question asked Jun 20, 2016 at 22:03 the hollathe holla 81611 silver badges17 bronze badges 1-
3
Well just do
const {thing1, …} = results; let {thing5} = results;
? – Bergi Commented Jun 20, 2016 at 22:15
2 Answers
Reset to default 8It 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
You still can use destructuring separately:
const results = yield getResults();
const { thing1, thing2, thing3, thing4} = results;
let { thing5 } = results;
Alternatively, it is possible to do
let thing5;
const { thing1, thing2, thing3, thing4 } = { thing5 } = yield getResults();
but I guess that should rather be avoided to reduce the WTF/minute of your code.
本文标签: javascriptES6 destructuring assignment with more than one variable typeStack Overflow
版权声明:本文标题:javascript - ES6 destructuring assignment with more than one variable type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743812949a2543406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论