The super() method is used when a class (child) inherits from another class (parent).
The super() method provides a means of syncing the child's constructor to the parent's constructor.
Let me illustrate with an example
classFoor{constructor(name){this.name=name}printName=()=>{console.log(this.name);}}classBarextendsFoo{constructor(name){super(name)}}constbar=newBar('Test');bar.printName()// Test
Now, looking at the code above,Foorequires anameto be passed to its constructor in order to function, whenBarinherited fromFoo, there's no way thenamecould be passed down toFooif not with thesuper()`
So, in relation to the snippet that you shared above, you are passing the message to the parent which in this case is Error, that way the child ErrorHandler has no access to message
Is there a necessity in Bar constructor at all?
You just pass the same Test value to the parent class ultimately. In this case
there is no need to a constructor at all I assume and Eslint should also hint about it.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great Article, Thanks
btw, I got a small question in this own Error class :
Why we have to write like this (1)
but not like this (2)
?
Actually, I wrote like (2) and then my own class lost a
message
propertyIt only comes back when I change it to (1).
What is the difference between them ?
Thanks,
The super() method is used when a class (child) inherits from another class (parent).
The super() method provides a means of syncing the child's constructor to the parent's constructor.
Let me illustrate with an example
FooNow, looking at the code above,
requires a
nameto be passed to its constructor in order to function, when
Barinherited from
Foo, there's no way the
namecould be passed down to
Fooif not with the
super()`So, in relation to the snippet that you shared above, you are passing the message to the parent which in this case is
Error
, that way the childErrorHandler
has no access tomessage
I hope this helps.
Is there a necessity in
Bar
constructor at all?You just pass the same
Test
value to the parent class ultimately. In this casethere is no need to a constructor at all I assume and Eslint should also hint about it.