Answer the question
In order to leave comments, you need to log in
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
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question