Yanor.net/
Wiki
Blog
GitHub
Sandbox
開始行:
* カスタムエラー [#d0e817be]
** ES2015 (ES6)以降 [#s1d83966]
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
- (1) nameの指定をしないと、
- (2) のtoString()が '''MyError: ERROR-MESSAGE'''ではなく...
** 参考 [#i1cfafbd]
- https://developer.mozilla.org/ja/docs/Web/JavaScript/Re...
- https://future-architect.github.io/typescript-guide/exc...
終了行:
* カスタムエラー [#d0e817be]
** ES2015 (ES6)以降 [#s1d83966]
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
- (1) nameの指定をしないと、
- (2) のtoString()が '''MyError: ERROR-MESSAGE'''ではなく...
** 参考 [#i1cfafbd]
- https://developer.mozilla.org/ja/docs/Web/JavaScript/Re...
- https://future-architect.github.io/typescript-guide/exc...
ページ名: