I always have such a intuition that an expression inside code can be mentally substituted by its evaluation result. However, the following two code snippets have different execution results.
let obj = {};
({a: obj.b, b: obj.a} = {a: 1, b: 2}); //* This line
console.log(obj); // {b: 1, a: 2}
versus
let obj = {a: 1, b: 2};
({a: obj.b, b: obj.a} = obj); //* and this line
console.log(obj); // {a: 1, b: 1}
I think the two starred line are same IN MY BRAIN, but they have different execution results.
Is this an intentional design?
2
Answers
Object destructuring is just syntactic sugar for this:
but here, source and target are the same object
Your first snippet is semantically equivalent to
Whereas your second snippet is semantically equivalent to