Both Babel and Traceur would transpile for the following code
obj = {
fullName: "Peter",
say() {
console.log("My name is", this.fullName);
}
};
let { fullName, say } = obj;
as
"use strict";
obj = {
fullName: "Peter",
say: function say() {
console.log("My name is", this.fullName);
}
};
var _obj = obj,
fullName = _obj.fullName,
say = _obj.say;
(Traceur uses the name $__1) the introduction of a new variable _obj seems totally unnecessary. What is a reason that they both do that?
When destructuring a variable declared with
varit is possible to reassign the variable containing the value you are currently destructuring.If this were naively transpiled without creating a temporary variable the variable foo would be altered, before the value for bar is retrieved:
My guess is that Babel has an optimization where it recognizes this as not necessary with
letbindings, because then you would have an error at the point of re-binding of the same variable. Evidently Traceur does not have this optimization. I'm not sure why either one wouldn't only use the local variable when the destructured variable is actually being re-bound.