Skip to content

Instantly share code, notes, and snippets.

@jaredhirsch
Last active December 23, 2015 07:19
Show Gist options
  • Save jaredhirsch/6599831 to your computer and use it in GitHub Desktop.
Save jaredhirsch/6599831 to your computer and use it in GitHub Desktop.
metaprogramming in JS for fun and profit
// remap exports.foo to exports._foo, log arguments
// passed to foo, then call _foo.
for (fun in exports) {
  (function(f) {
    // bail if it's not a function I added to the exports object
    if (typeof exports[f] !== 'function' || !exports.hasOwnProperty(f)) { return; }
    exports['_' + f] = exports[f];
    exports[f] = function() {
      logger.debug("lib::db::cassandra." + f + " intercepted. Args: " + JSON.stringify(arguments, null, ' '));
      exports['_' + f].apply(exports, arguments);
    }
  })(fun)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment