super() on class that extends

Sébastien Cevey seb.cevey at guardian.co.uk
Sat Apr 11 13:09:31 UTC 2015


Does the same apply to subclassing Error?

I've been trying to do that but it doesn't seem to work as expected, in
particular passing the message as argument to `super` doesn't seem to do
anything.

For example:

``` javascript
class Foo extends Error {
  constructor(message) {
    super(message);
  }
}

var err1 = new Foo("arg");
console.log(err1); // plain Foo object
console.log(err1.message); // "" (empty string!?)

var err2 = new Error("arg");
console.log(err2); // Error object
console.log(err2.message); // "arg"
```

The above is true both with Traceur and Babel.

https://google.github.io/traceur-compiler/demo/repl.html#class%20Foo%20extends%20Error%20%7B%0A%20%20constructor(message)%20%7B%0A%20%20%20%20super(message)%3B%0A%20%20%7D%0A%7D%0A%0Avar%20err1%20%3D%20new%20Foo(%22arg%22)%3B%0Aconsole.log(err1)%3B%20%2F%2F%20plain%20Foo%20object%0Aconsole.log(err1.message)%3B%20%2F%2F%20%22%22%20(empty%20string!%3F)%0A%0Avar%20err2%20%3D%20new%20Error(%22arg%22)%3B%0Aconsole.log(err2)%3B%20%2F%2F%20Error%20object%0Aconsole.log(err2.message)%3B%20%2F%2F%20%22arg%22
https://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=class%20Foo%20extends%20Error%20%7B%0A%20%20constructor(message)%20%7B%0A%20%20%20%20super(message)%3B%0A%20%20%7D%0A%7D%0A%0Avar%20err1%20%3D%20new%20Foo(%22arg%22)%3B%0Aconsole.log(err1)%3B%20%2F%2F%20plain%20Foo%20object%0Aconsole.log(err1.message)%3B%20%2F%2F%20%22%22%20(empty%20string!%3F)%0A%0Avar%20err2%20%3D%20new%20Error(%22arg%22)%3B%0Aconsole.log(err2)%3B%20%2F%2F%20Error%20object%0Aconsole.log(err2.message)%3B%20%2F%2F%20%22arg%22

On Sat, 11 Apr 2015 at 03:30 Axel Rauschmayer <axel at rauschma.de> wrote:

> No engine has implemented subclassing of `Array`, yet:
> http://kangax.github.io/compat-table/es6/#Array_is_subclassable
>
> And, as Sebastian mentioned, you can’t transpile it, because it depends on
> the cooperation of `Array`: it becomes the base constructor and allocates
> an exotic array instance (with special handling for `length` etc.) whose
> prototype is `new.target`.
>
> Axel
>
>
> On 11 Apr 2015, at 03:57, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
>
> On 4/10/15, Axel Rauschmayer <axel at rauschma.de> wrote:
>
> The reason why you need to call the super-constructor from a derived class
> constructor is due to where ES6 allocates instances - they are allocated
> by/in the base class (this is necessary so that constructors can be
> subclassed that have exotic instances, e.g. `Array`):
>
> Can you please explain how extending Array works. Also what is the
> optional identifier optional for in ClassExpression:
>
> var myArray = (new class B extends Array {
>   constructor() {
>     super(1,2,3,4,5);
>  }
> });
> alert(myArray.length); // it's 0 in Babel.
>
> --
> Garrett
> @xkit
> ChordCycles.com
> garretts.github.io
> personx.tumblr.com
>
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
> rauschma.de
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>

-- 

------------------------------
Visit theguardian.com. On your mobile and tablet, download the Guardian 
iPhone and Android apps theguardian.com/guardianapp and our tablet editions 
theguardian.com/editions.  Save up to 57% by subscribing to the Guardian 
and Observer - choose the papers you want and get full digital access. 
 Visit subscribe.theguardian.com

This e-mail and all attachments are confidential and may also be 
privileged. If you are not the named recipient, please notify the sender 
and delete the e-mail and all attachments immediately. Do not disclose the 
contents to another person. You may not use the information for any 
purpose, or store, or copy, it in any way.  Guardian News & Media Limited 
is not liable for any computer viruses or other material transmitted with 
or as part of this e-mail. You should employ virus checking software.
 
Guardian News & Media Limited is a member of Guardian Media Group plc. Registered 
Office: PO Box 68164, Kings Place, 90 York Way, London, N1P 2AP.  Registered 
in England Number 908396


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150411/781e493e/attachment.html>


More information about the es-discuss mailing list