ES6 destructuring assignment with more than one variable type

前端 未结 2 1793
情书的邮戳
情书的邮戳 2021-01-13 04:45

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 declar

相关标签:
2条回答
  • 2021-01-13 05:29

    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
    
    0 讨论(0)
  • 2021-01-13 05:31

    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.

    0 讨论(0)
提交回复
热议问题