Patches the specified method methodName
of the class cls
with a new method
newMethodFactory
should be a function which returns a new method for methodName
.
* It's recommended to use Patcher2
, which is a simpler patcher with a user-friendly interface
class HelloClass {
a = 1;
hello(){
console.log("Hello", this.a);
}
}
const helloobj = new HelloClass();
maginai.patcher.patchMethod(
HelloClass, // Patches the `hello` method of `HelloClass`...
"hello",
(origMethod) => {
// The new `hello` method...
const rtnFn = function (...args) {
// calls the original method and then says 'Bye'
const rtn = origMethod.call(this, ...args);
console.log("Bye");
return rtn;
// The original `hello` definition currently has no args, but we passed args for compatibility.
// For the same reason, it's recommended to return the original value (or you changed) even if the original method currently has no return value.
};
return rtnFn;
})
// After the patching, even already instanciated objects use the new method
helloobj.hello()
// Hello 1
// Bye
the class which has the method methodName
the name of the method to be patched
a function which returns a new method for methodName
origMethod
is the original method methodName
maginai.patcher
submodule classDo not instantiate directly; use from
maginai.patcher
.You can patch a method of class with
patchMethod
method