Skip to content

Instantly share code, notes, and snippets.

@chestozo
Created May 5, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chestozo/478e1b7f901fd8b96fbf39fc4a7bb114 to your computer and use it in GitHub Desktop.
Save chestozo/478e1b7f901fd8b96fbf39fc4a7bb114 to your computer and use it in GitHub Desktop.
function wrap(obj, onlyOwnProperties) {
obj = obj || {};
for (var propName in obj) {
if (onlyOwnProperties && obj.hasOwnProperty(propName)) {
continue;
}
if (typeof obj[propName] !== 'function') {
continue;
}
wrapMethod(obj, propName);
}
}
function wrapMethod(obj, methodName) {
var _method = obj[methodName];
var counter = 0;
obj[methodName] = function() {
var result = _method.apply(obj, arguments);
var index = ++counter;
console.log(index, methodName, arguments, '>>>', result);
return result;
};
for (var prop in _method) {
if (_method.hasOwnProperty(prop)) {
obj[methodName][prop] = _method[prop];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment