function mPatch(object, fnProperty, { onBefore, onAfter }) { const originFn = object[fnProperty]; object[fnProperty] = (...args) => { if (typeof onBefore === 'function') { onBefore(...args); } const returnValue = originFn(...args); if (typeof onAfter === 'function') { onAfter(returnValue); } return returnValue; }; return () => { object[fnProperty] = originFn; }; } function withLogger(object, fnProperty) { return mPatch(object, fnProperty, { onBefore: args => console.log(`${fnProperty} args:`, args), onAfter: returnValue => console.log(`${fnProperty} returnValue`, returnValue) }); }