It looks like Babel is taking some invalid shortcuts here. The
DestructuringAssignmentEvaluation runtime semantics[1] require the same
source object (the parameter `value`) to be used throughout the
destructuring operation. Babel compiles the first destructuring into

var bar = foo.bar;
var qaz = foo.qaz;

So `foo` gets looked up twice, with the second lookup resulting in a
different value, the one that the `bar` getter assigned to `foo`. In
SpiderMonkey, both getters get called (you can check this with console.log
calls in the getters) because the destructuring operates on the same value
throughout.

Curiously, the inline destructuring in the console.log call at the end
fares better here: it assigns `foo` to a temporary variable `_foo`, so
changing the value of `foo` doesn't influence results. (I do wonder if that
causes `foo` to be leaked forever, though: `_foo` is a global variable
defined at the script's top.)

[1]
http://www.ecma-international.org/ecma-262/6.0/index.html#sec-runtime-semantics-destructuringassignmentevaluation

On Mon, Jul 20, 2015 at 10:58 AM, Michał Wadas <[email protected]>
wrote:

> Following code:
>
>
> var foo = {
>   get bar() {
>     foo = {
>       qaz: 'bar-r'
>     };
>     return 'bar-c';
>   },
>   get qaz() {
>     foo = {
>       bar: 'qaz-r'
>     }
>     return 'qaz-c';
>   }
> };
>
> let {bar, qaz} = foo;
> console.log(({bar, qaz} = foo));
>
> Firefox:
> Object { bar: "qaz-r" }
> Babel:
> Object { qaz: "bar-r" }
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to