R
R
romaro2021-07-20 14:27:54
JavaScript
romaro, 2021-07-20 14:27:54

Why duplicate the property that was passed to super()?

When studying the source code of one project , I found the following code:

class AssertionError extends Error {
  constructor (message) {
    super(message)
    this.message = message || 'Assertion error'
    this.code = 'ASSERTION_ERROR'
    this.status = 500
  }
}


Do I understand correctly that the message property is duplicated in the inherited class so that its value can be obtained from the parent class only if it was explicitly set when calling AssertionError. At the same time, the AssertionError property had to be defaulted.

Or did I miss something about super()?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
EgorSvinarev, 2021-07-21
@EgorSvinarev

The super construct is needed in order to pass the necessary arguments to the constructor of the parent class. In this case, the constructor of the parent class Error takes a string indicating the error message as arguments. If an empty string is passed to the constructor of the AssertionError class, then the message field will be filled with the value "Assertion error".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question