Timeline for What's a good way to extend Error in JavaScript?
Current License: CC BY-SA 4.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 27, 2018 at 1:18 | history | edited | Onur Yıldırım | CC BY-SA 4.0 |
update summary
|
Jan 5, 2017 at 3:20 | history | edited | Onur Yıldırım | CC BY-SA 3.0 |
Updated code, added test links
|
Jan 3, 2017 at 22:40 | history | edited | Onur Yıldırım | CC BY-SA 3.0 |
capture stack with `CustomError` instead of `this.constructor`
|
Jan 3, 2017 at 1:48 | comment | added | Matt Browne | Thanks. I replied to your comment on the gist (just in case you missed it - no need to reply unless you have something more to add). | |
Dec 27, 2016 at 20:36 | comment | added | Onur Yıldırım | Thanks. I continued the discussion there below the Gist. | |
Dec 27, 2016 at 20:36 | history | edited | Onur Yıldırım | CC BY-SA 3.0 |
Changed back to Object.setPrototypeOf(CustomError.prototype, Error.prototype);
|
Dec 27, 2016 at 2:22 | comment | added | Matt Browne |
I created a gist demonstrating why using Object.setPrototypeOf doesn't make sense here, at least not in the way you're using it: gist.github.com/mbrowne/4af54767dcb3d529648f5a8aa11d6348. Perhaps you meant to write Object.setPrototypeOf(CustomError.prototype, Error.prototype) - that would make slightly more sense (although still providing no benefit over simply setting CustomError.prototype ).
|
|
Dec 23, 2016 at 19:03 | comment | added | Onur Yıldırım |
CustomError.prototype = Object.create(Error.prototype) is also changing the prototype. You have to change it since there is no built-in extend/inherit logic in ES5. I'm sure the babel plugin you mention does similar things.
|
|
Dec 23, 2016 at 14:45 | comment | added | Matt Browne |
My point was that I don't think you actually need to change the prototype here. You could simply use your line at the bottom (CustomError.prototype = Object.create(Error.prototype) ). Also, Object.setPrototypeOf(CustomError, Error.prototype) is setting the prototype of the constructor itself rather than specifying the prototype for new instances of CustomError . Anyway, in 2016 I think there's actually a better way to extend errors, although I'm still figuring out how to use it together with Babel: github.com/loganfsmyth/babel-plugin-transform-builtin-extend/…
|
|
Dec 23, 2016 at 11:58 | comment | added | Onur Yıldırım |
Changing the prototype of an object is discouraged all together, not setPrototypeOf . But if you still need it (as OP asks), you should use the built-in methodology. As MDN indicates, this is considered the proper way to set the prototype of an object. In other words, MDN says do not change the prototype (as it affects performance and optimization) but if you have to, use setPrototypeOf .
|
|
Dec 22, 2016 at 21:23 | comment | added | Matt Browne |
Why are you using setPrototypeOf() ? At least according to MDN, it's generally discouraged to use it if you can accomplish the same thing by just setting the .prototype property on the constructor (as you're doing in the else block for browses that don't have setPrototypeOf ).
|
|
Dec 18, 2016 at 18:30 | history | edited | Onur Yıldırım | CC BY-SA 3.0 |
Updated code
|
Mar 9, 2016 at 1:57 | history | edited | Onur Yıldırım | CC BY-SA 3.0 |
deleted 34 characters in body
|
Mar 9, 2016 at 1:52 | history | answered | Onur Yıldırım | CC BY-SA 3.0 |