+ 4
when we use super() in class ?
Example code:- class Animal { constructor(name) { this.name = name; } speak() { console.log(this.name + ' makes a noise.'); } } } class Dog extends Animal { speak() { super.speak(); // Super console.log(this.name + ' barks.'); } } let dog = new Dog('Rex'); dog.speak(); // what are the usages of super() ?
1 Antwort
+ 3
The super keyword is used to access and call methods on an object's parent.
For example super()
would call the parent class contractor()
And
super.foo()
would call the parent class method, foo().