カスタムエラーES2015 (ES6)以降class MyError extends Error { constructor(message) { super(message); this.name = new.target.name; // (1) } } const err = new MyError("ERROR-MESSAGE"); console.log(err.toString()); // (2) if (err instanceof MyError){ console.log("OK"); } ↓ ↓ ↓ MyError: ERROR-MESSAGE OK
参考 |
|