util-ex
Version:
Browser-friendly enhanced util fully compatible with standard node.js
33 lines (32 loc) • 1.08 kB
TypeScript
/**
* Injects method into an object. optionally preserving access to the original method via "`super`" and original instance via "`self`".
*
* **Note**:
*
* * In the new method, you can use `this.super()` to call the original method, `this.super()` is already bound with original instance.
* * The `this[aMethodName]` is also the original method, but not bound yet.
* * `this.self` is the original instance!
*
* @param {object} aObject the target object to inject
* @param {string} aMethodName the target method to inject
* @param {Function} aNewMethod the new method to be injected into the aObject.
* @returns {boolean} whether the injection is successful.
*
* @example
* var obj = {
* method1: function() {
* console.log('Hello');
* }
* };
*
* var newMethod = function() {
* this.super();
* console.log('World');
* };
*
* injectMethod(obj, 'method1', newMethod);
*
* obj.method1(); // Output: Hello\nWorld
*/
export function injectMethod(aObject: object, aMethodName: string, aNewMethod: Function): boolean;
export default injectMethod;