A new ES6 draft is available
Allen Wirfs-Brock
allen at wirfs-brock.com
Fri Jan 16 12:02:57 PST 2015
On Jan 16, 2015, at 11:40 AM, Kevin Smith wrote:
> Changes include: Updated specification to use and support the new built-in subclassing scheme described at: https://github.com/tc39/ecma262/blob/master/workingdocs/ES6-super-construct%3Dproposal.md
>
> This looks nice. An interesting question:
>
> For classes that have divergent [[Construct]]/[[Call]] behavior, should the [[Call]] behavior be "inherited" by derived classes?
>
> class MyDate extends Date {}
> console.log(MyDate(1, 2, 3)); A string, or throw an error?
>
>
Since the above class definition does not include an explicit constructor body, it gets the equivalent of
constructor(...args) {super(...args)}
as its implicit constructor definition
A 'super()' call throws if the NewTarget is null (ie, if the constructor is invoked via [[Call]] )
If you want to inherit call behavior you need to code it as:
class MyDate extends Date {
constructor(...args) {
if (new.target===null) return super.constructor(...args)
super(...args);
}
}
That's assuming 'new.target' makes it into ES6. Without it you would have to do something like:
class MyDate extends Date {
constructor(...args) {
let calledAsFunction=true;
try {
let thisValue = this; //can't reference 'this' prior to 'super()' in a [[Construct]] call of a derived function
} catch (e} {
let calledAsFunction = false
}
if (calledAsFunction) return super.constructor(...args)
super(...args);
}
}
Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150116/f8605b1a/attachment.html>
More information about the es-discuss
mailing list