Allen Wirfs-Brock (2013-07-10T23:56:07.000Z)
On Jul 10, 2013, at 4:32 PM, Andrea Giammarchi wrote:

> wrong at least two things for the non ES6 version
> 
>  1. `Error.call(this, message)` does not do what you think it should do
>  2. `CustomError.prototype = Object.create(Error)` has a typo, you meant `Object.create(Error.prototype)`
> 
> In the ES6 version, I am not sure about constructors and classes, but name should be inherited or the console won't even look for it as own property so it should be
> 
> ```javascript
> class CustomError extends Error {
>   name = 'CustomError'
>   constructor (message /* accept any custom arguments here */) {
>     super(message)
>   }
> }
> ```
> only in case CustomError won't have already a `name` property as it is for ES3<=ES5 constructors.

You need to say:

class CustomError extends Error {
  get name() {return 'CustomError'}
  constructor (message /* accept any custom arguments here */) {
    super(message)
  }
}

or alternatively:

class CustomError extends Error {
  constructor (message /* accept any custom arguments here */) {
    super(message)
  }
}
CustomError.prototype.name = 'CustomError';

This is one of the rare places where it is inconvenient that class declarations don't provide syntax for defining data properties on the prototype.

Forbes class version should be fine if you don't mind each CustomError instance having an own 'name' data property.

Allen


> 
> :-)
> 
> 
> 
> 
> On Wed, Jul 10, 2013 at 4:22 PM, Forbes Lindesay <forbes at lindesay.co.uk> wrote:
> What's wrong with:
> 
> ```javascript
> 
> throw new CustomError('message')
> 
> function CustomError(message /* accept any custom arguments here */) {
>   Error.call(this, message)
>   this.name = 'CustomError'
>   /**
>    * add any custom error info here
>    */
> }
> CustomError.prototype = Object.create(Error)
> CustomError.prototype.constructor = CustomError
> ```
> 
> In ES6 land this becomes:
> 
> 
> ```javascript
> 
> throw new CustomError('message')
> 
> class CustomError extends Error {
>   constructor (message /* accept any custom arguments here */) {
>     super(message)
>     this.name = 'CustomError'
> 
>     /**
>      * add any custom error info here
>      */
>   }
> }
> ```
> 
> Doesn't that seem like an obvious enough solution?
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130710/9a09f689/attachment.html>
forbes at lindesay.co.uk (2013-07-12T03:37:00.634Z)
You need to say:

```js
class CustomError extends Error {
  get name() {return 'CustomError'}
  constructor (message /* accept any custom arguments here */) {
    super(message)
  }
}
```

or alternatively:

```js
class CustomError extends Error {
  constructor (message /* accept any custom arguments here */) {
    super(message)
  }
}
CustomError.prototype.name = 'CustomError';
```

This is one of the rare places where it is inconvenient that class declarations don't provide syntax for defining data properties on the prototype.

Forbes class version should be fine if you don't mind each CustomError instance having an own 'name' data property.