admin管理员组文章数量:1426072
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);
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);
Share
Improve this question
edited Mar 24, 2016 at 16:08
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Mar 24, 2016 at 15:13
CapybaraCapybara
1907 bronze badges
2
-
2
I don't understand the response to that bug; a
super()
call is most certainly allowed to include an argument list, and the Object constructor is supposed to pay attention to its argument. – Pointy Commented Mar 24, 2016 at 15:27 -
1
It's a Chromium bug. I've tested on Microsoft Edge. It returns
true
. – Lewis Commented Mar 24, 2016 at 15:35
1 Answer
Reset to default 9In 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.
本文标签:
版权声明:本文标题:javascript - super() does not pass arguments when instantiating a class extended from Object in Chrome V8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745382571a2656231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论