ES2015 Object of destruction twice in the same area

Is there a clean way to destroy the same variables from two similar objects in the same area?

function(oldState, newState) { let {foo, bar} = oldState; // do stuff // let {foo, bar} = newState; // illegal double declaration in same scope {foo, bar} = newState; // illegal, not sure why let {foo: foo1, bar: bar1} = newState; // legal but ugly foo = newState.foo; // legal, but requires multiple lines } 
+6
source share
1 answer

You can transfer assignment to parens to reassign variables through destructuring. The reason this is necessary is because otherwise the parser { taken by the parser to start the block, not the object literal or destination pattern. This blog post explains the situation in more detail.

 function(oldState, newState) { let {foo, bar} = oldState; // do stuff // ({foo, bar} = newState); } 
+8
source

All Articles