oh well ... I've missed the part when you guys decided that inherited defaults are useless as properties ... :-/
as you said, inconvenient, and not only this case ... also good to know ^_^ On Wed, Jul 10, 2013 at 4:56 PM, Allen Wirfs-Brock <[email protected]>wrote: > > 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 <[email protected]>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 >> [email protected] >> https://mail.mozilla.org/listinfo/es-discuss >> > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

