I am fairly new to js and I have a problem that seems I can solve by modifying the constructor of a class. This does not work
// original class in lib
class BasePrinter {
constructor() {
console.log("BasePrinter constructor");
}
}
// patching
function patchBasePrinter() {
const originalConstructor = BasePrinter.prototype.constructor;
BasePrinter.prototype.constructor = function(...args) {
console.log("Patched constructor logic before");
originalConstructor.apply(this, args);
console.log("Patched constructor logic after");
};
}
// Apply the patch
patchBasePrinter();
const printer = new BasePrinter();
The other posts ive seen on this are either inconclusive or end with a solution of patching a method instead.
The real use case is to apply new logic when any class property is changed. The system is already reactive. I just want to add new logic and all the techniques I can find have to do with patching the constructor class