How to extend many Error classes dynamically?

Darius

I can declare extended Error class like:

class NotFoundError extends Error {
  constructor (message) {
    super(message)
    this.name = 'NotFoundError' // for log
  }
}

and then evaluate:

if (error instanceof NotFoundError) {
  ...
}

How can I declare many errors dynamically, preferably in ES5 syntax?

declareErrors(['NotFoundError', 'TimeoutError', 'AclError', ...])
Amadan

Easy, use an anonymous class expression and assign it to a global variable:

function declareError(name) {
  window[name] = class extends Error {
    constructor (message) {
      super(message)
      this.name = name;
    }
  }
}
function declareErrors(errors) {
  errors.forEach(declareError);
}
declareErrors(['NotFoundError', 'TimeoutError', 'AclError']);

const error = new AclError("Bad Darius!");
console.log(
    error.name,
    error.message,
    error instanceof Error,
    error instanceof AclError
);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How many (classes of) objects can YOLO detect?

分類Dev

How to I dynamically add a pytest marker with my own pytest classes?

分類Dev

How many singleton classes can we use in one android app?

分類Dev

2 Classes, extend same class, can this be simplified?

分類Dev

Correct way to extend classes with Symfony autowiring

分類Dev

how to change TextInputLayout error color and Text size of Error dynamically with code

分類Dev

Docker on Windows using Hyper-V. How to dynamically added space to docker or extend to harddrive when heavy Images pulled

分類Dev

javascript dynamically adding and removing classes

分類Dev

How to extend a React component?

分類Dev

How to extend a React component?

分類Dev

How to extend a React component?

分類Dev

How to extend BitBake class

分類Dev

How many equivalence classes in the RL relation for {w in {a, b}* | (#a(w) mod m) = ((#b(w)+1) mod m)}

分類Dev

How to disable one transformer validation error "dynamically" in Symfony2

分類Dev

Typescript: Extend interface dynamically by adding new fields with certain naming

分類Dev

AngularJs validator with a form of many dynamically created inputs

分類Dev

How to extend my root (/) partition?

分類Dev

How to extend ElementFinder object in ProtractorJS?

分類Dev

How to extend a Generic Collection in Delphi?

分類Dev

Android Kotlin - How to extend ConstraintLayout?

分類Dev

Javascript composition how to extend an object

分類Dev

How to extend a parent babel config?

分類Dev

How to extend html file in Django

分類Dev

Hibernate - multiple many-to-many associations between two classes

分類Dev

flask many-to-many-relation leads to "Multiple classes found for path"

分類Dev

Updating one JTextPane out of many different classes

分類Dev

Calling the same function in many different classes

分類Dev

Linker error with template classes

分類Dev

Error In classes C++

Related 関連記事

  1. 1

    How many (classes of) objects can YOLO detect?

  2. 2

    How to I dynamically add a pytest marker with my own pytest classes?

  3. 3

    How many singleton classes can we use in one android app?

  4. 4

    2 Classes, extend same class, can this be simplified?

  5. 5

    Correct way to extend classes with Symfony autowiring

  6. 6

    how to change TextInputLayout error color and Text size of Error dynamically with code

  7. 7

    Docker on Windows using Hyper-V. How to dynamically added space to docker or extend to harddrive when heavy Images pulled

  8. 8

    javascript dynamically adding and removing classes

  9. 9

    How to extend a React component?

  10. 10

    How to extend a React component?

  11. 11

    How to extend a React component?

  12. 12

    How to extend BitBake class

  13. 13

    How many equivalence classes in the RL relation for {w in {a, b}* | (#a(w) mod m) = ((#b(w)+1) mod m)}

  14. 14

    How to disable one transformer validation error "dynamically" in Symfony2

  15. 15

    Typescript: Extend interface dynamically by adding new fields with certain naming

  16. 16

    AngularJs validator with a form of many dynamically created inputs

  17. 17

    How to extend my root (/) partition?

  18. 18

    How to extend ElementFinder object in ProtractorJS?

  19. 19

    How to extend a Generic Collection in Delphi?

  20. 20

    Android Kotlin - How to extend ConstraintLayout?

  21. 21

    Javascript composition how to extend an object

  22. 22

    How to extend a parent babel config?

  23. 23

    How to extend html file in Django

  24. 24

    Hibernate - multiple many-to-many associations between two classes

  25. 25

    flask many-to-many-relation leads to "Multiple classes found for path"

  26. 26

    Updating one JTextPane out of many different classes

  27. 27

    Calling the same function in many different classes

  28. 28

    Linker error with template classes

  29. 29

    Error In classes C++

ホットタグ

アーカイブ