try {
const { attribute1, attribute2 } = getObject()
} catch () {}
attribute1 // undefined
How would you solve that? try {
const { attribute1, attribute2 } = getObject()
} catch () {}
attribute1 // undefined
How would you solve that?OTOH var declared inside is a bad solution because it's both misleading as to scope and using a freely-assignable variable for something that's logically single-assignment. Let outside has only one of those problems.
I usually solve that case with
let attribute1, attribute2;
try {
const object = getObject()
attribute1 = object.attribute1
attribute2 = object.attribute2
} catch () {}
But I'm just not satisfied with it, too many statements. And it gets really messy with many attributes let attribute1, attribute2;
try {
({attribute1, attribute2} = getObject())
} catch (e) {}So parenthesis are the trick, thanks!