How i can override parent method on child ? Is javascript have something like parent::method();
var Parent = function(){
this.myMethod(){
//Some code here
};
}
var Child = function(){
Parent.call(this);
this.myMethod(){
//Some code here
//and then run parent method
};
}
Updated There is better way than do that (not ES6)?:
var Parent = function ()
{
this.myMethod()
{
this.extedMyMethod()
//Some code here
};
this.extedMyMethod()
}
var Child = function ()
{
Parent.call(this);
this.extedMyMethod()
{
//Some code which expand parent method
};
}
P.S. If i do like @Suren Srapyan suguest webpack will convert to proper non ES6 ?
To override a method, we give it the same name and parameters as the method in the superclass. We can override receiveDamage() from the superclass by coding the same method in the subclass.
Otherwise, use a simple structure with methods declared with prototype, you can get a better performance. var MyObject = (function () { // Constructor function MyObject (foo) { this. _foo = foo; } function privateFun (prefix) { return prefix + this. _foo; } MyObject.
The super keyword is used to call the constructor of its parent class to access the parent's properties and methods.
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.
With ES6 class syntax inheritance you can do it via super
calls.
class Parent{
method(){
console.log('Parent !!!');
}
}
class Child extends Parent{
constructor(){
super();
}
method(){
console.log('Child !!!');
super.method();
}
}
var child = new Child();
child.method();
UPDATED
You need also to use polyfill for different browsers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With