class bcomp {
some(){
console.log("bcomp");
}
}
class acomp extends bcomp {
some(){
super.some();
console.log("acomp");
}
}
var ins = new acomp();
ins.some()
so output will be
bcomp
acomp
I need to override some method of acomp
with super
keyword
acomp.prototype.some = function (params) {
super.some();
console.log("new acomp");
}
SyntaxError: 'super' keyword unexpected here
How to achieve the above?
3
Answers
Call the
some
method of the parent class directly like thisinstead of
super.some()
A generic patch approach for
some
which targets an objects 2nd prototype within this objects prototype chain without having to know the object’s/instance’s involved classes would make use ofObject.getPrototypeOf
twice.In addition one should make use of
Reflect.getOwnPropertyDescriptor
together withReflect.defineProperty
in order to patch this newsome
method in its most correct way.JS is a prototypical language but at the same time it is a functional language. It’s best not to modify the prototype methods once they are set. As i remember they are costly operations. Instead it’s a reasonable approach to make the
some
function to accept the desired functionality.