0

I am just starting to write in ES6 after years of rigidity sticking to plain ole JS (ES5). One feature that I really like is Object Destructuring while declaring and assigning variables :

function someFunction() {
    return Promise.resolve({
        a: 0,
        b: 1
    });
}
let {a, b} = await someFunction();

But there is a particular case where I'd love to transform existing variables like this :

function otherFunction(a, b) {
    return Promise.resolve({
        a: a + 1,
        b: b + 2
    });
}

let {a, b} = await someFunction();
{a, b} = await otherFunction(a, b);

Since the two variables were already declared using let I can't redeclare them. If I switch back to using var instead of let, I can do that :

var {a, b} = await someFunction();
var {a, b} = await otherFunction(a, b);

Which works perfectly fine but I want to benefit from the strictness of ES6 and the let keyword, is there a way to destructure-assign without declaring ?

Mouradif
  • 2,666
  • 1
  • 20
  • 37
  • Notice that you can also do `const {a: newA, b: newB} = …` – Bergi May 11 '18 at 13:00
  • What I'm trying to do is reassign `a` and `b` that have already been assigned on the previous line so without re-using `let` let alone `const` since it's definitely a variable I want (some identified which value is bound to change). – Mouradif May 11 '18 at 13:41
  • The answer you have marked answers my question well but I couldn't find it before asking. Should I delete my question or keep it for future reference ? – Mouradif May 11 '18 at 13:43
  • You can keep it up, nothing wrong with it. (Admittedly, maybe you should have found the very similarly titled [How to destruct an object to an already defined variable?](https://stackoverflow.com/q/32138513/1048572)) – Bergi May 11 '18 at 13:44
  • Wow weird that it wasn't suggested in the quick search before asking my question ! Thanks anyway – Mouradif May 11 '18 at 15:00
  • 1
    Maybe because you used the correct term "destructure", not just "destruct". Let's hope your question will be found in the search from now on! – Bergi May 11 '18 at 15:54

0 Answers0