Clean way to keep original variable and destructure at the same time
One possible way:
const result = doSomething(),
{ a, b } = result;
You still have to duplicate the name, though. const
token isn't quite right-handy. )
Idea 1
Create this helper function:
function use(input, callback) {
callback(input, input);
}
and use it like:
use(doSomething(), (result, {a, b}) => {
// Do something with result as a whole, or a and b as destructured properties.
});
For example:
use ({a: "Hello", b: "World", c: "!"}, (result, {a, b}) => {
console.log(result);
console.log(a);
console.log(b);
});
// generates
// {a: "Hello", b: "World", c: "!"}
// Hello
// World
They're not
const
, but they're scoped, for better or worse!
Idea 2
Combine array
and object
deconstruction. Create this helper function:
const dup = input => [input, input];
And then deconstruct away like so:
const [result, {a, b}] = dup(doSomething());
Now, your
result
,a
, andb
are allconst
s.