overriding methods in javascript classes code example
Example 1: javascript class inheritance
class Mammal {
constructor(name) {
this.name = name;
}
eats() {
return `${this.name} eats Food`;
}
}
class Dog extends Mammal {
constructor(name, owner) {
super(name);
this.owner = owner;
}
eats() {
return `${this.name} eats Chicken`;
}
}
let myDog = new Dog("Spot", "John");
console.log(myDog.eats());
Example 2: override function javascript class
class newArray extends Array{
push(value) {
console.log(`pushed ${value} on to array`)
super.push(value)
}
}
var array = new newArray
array.push('new Value')