Advertisement
kstoyanov

05. Spy

Oct 4th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function spy(obj, methodName) {
  2.   const spy = { count: 0 };
  3.   const currentMethod = obj[methodName];
  4.  
  5.   if (!currentMethod) { return; }
  6.  
  7.   obj[methodName] = function (...args) {
  8.     this.count++;
  9.     return currentMethod.call(obj, ...args);
  10.   }.bind(spy);
  11.   return spy;
  12. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement