How to override inherited methods when using JavaScript subclasses of ES6 / ES2015

I created a class that extends an array. I want to execute arbitrary code before calling the inherited push function.

class newArray extends Array{ //execute any logic require before pushing value onto array this.push(value) } 
+8
override inheritance ecmascript-6 class subclass
source share
1 answer

The solution I found was to create a new function in a subclass with the same name as the inherited function. In this case, click. Then, inside the overriding function, the inherited function is called through the super keyword.

 class newArray extends Array{ push(value) { //execute any logic require before pushing value onto array console.log(`pushed ${value} on to array`) super.push(value)} } var array = new newArray array.push('new Value') 
+21
source share

All Articles