Created
April 21, 2018 04:42
-
-
Save sophistifunk/e8db29e2aca3f70ec1a649df0cbd38fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo extends Error { | |
type: string; | |
constructor(type: string, message: string) { | |
super(message); | |
// Clean up the changes made by super() | |
this.constructor = Foo; | |
if ((Object as any).setPrototypeOf) { | |
(Object as any).setPrototypeOf(this, Foo.prototype); | |
} else { | |
(this as any).__proto__ = Foo.prototype; | |
} | |
// Only reference props on this after this point for safety | |
this.type = type; | |
} | |
doStuff() { | |
return "I am a " + this.type + " type of error." | |
} | |
} | |
let e = new Foo("dramatic", "Goodbye, cruel world"); | |
console.clear(); | |
console.log('e instanceof Error', e instanceof Error); | |
console.log('e instanceof Foo ', e instanceof Foo); | |
console.log('e.type ', e.type); | |
console.log('typeof e.doStuff ', typeof e.doStuff); | |
console.log(''); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment