The code below logs false in Chrome V8 but logs true in Babel. The feedback from Google said that logging false is how it is supposed to be while logging true is a bug of Babel. I looked into the ES6 specs and still couldn't understand the mechanism behind this. Any thoughts would be appreciated!
class NewObj extends Object{
constructor(){
super(...arguments); // In V8, after arguments === [{attr: true}]
// is passed as parameter to super(),
// this === NewObj{} in V8;
// but this === NewObj{attr: true} in Babel.
}
}
var o = new NewObj({attr: true});
console.log(o.attr === true);
In fact the feedback you got on that Chrome bug is correct. Some things did change here from ES5 to ES6. Babel can't really do anything about it, and Edge didn't yet change it. Or it's an erratum :-)
The ES5 new Object(value)
spec says that it returns an object value that is passed in.
In ES6, you have to check the sections on [[construct]]
of builtins and the Object(value)
function. And the first statement of that is
If NewTarget is neither
undefined
nor the active function, then
Return OrdinaryCreateFromConstructor(NewTarget, "%ObjectPrototype%").
So if you are doing new Object({…})
, it will still return the {…}
argument. But when you are calling it from super()
or as Reflect.construct(Object, [{…}], MyCustomTarget)
, so that new.target
is not Object
, then you'll just get a new object constructed from MyCustomTarget.prototype
, and the argument is ignored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With