我创建了一个扩展Array的类。我想在调用继承的push函数之前执行任意代码。
class newArray extends Array{
//execute any logic require before pushing value onto array
this.push(value)
}
我发现的解决方案是在子类中创建一个与继承函数同名的新函数。在这种情况下推。然后在覆盖函数内部通过super关键字调用继承的函数。
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')