Created
February 27, 2012 18:32
-
-
Save johntimothybailey/1926039 to your computer and use it in GitHub Desktop.
Log Method Invocation - An Underscore Mixin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.mixin({ | |
| /** | |
| * Logs method invocation of a provided instance | |
| * | |
| * @param target Currently must be an instance | |
| * @param options Possible options are as follows | |
| * prefix - A prefix String for the logging (e.g. "Person instance") | |
| * logMethod - The function to use for doing the logging (default: console.log) | |
| */ | |
| logMethodInvocation : function(target,options) { | |
| _.each(_.methods(target),function(method){ | |
| options = options || {}; | |
| if(_.isString(options)){ | |
| options = { | |
| prefix: options | |
| }; | |
| } | |
| var _method = target[method]; | |
| target[method] = function(){ | |
| options.logMethod = options.logMethod || console.log; | |
| if(options.logMethod){ | |
| options.prefix = options.prefix || ""; | |
| options.logMethod(options.prefix,method,arguments); | |
| } | |
| return _method.apply(target,arguments); | |
| }; | |
| }); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _.logMethodInvocation(_,"underscore method:"); | |
| // OR | |
| _.logMethodInvocation(_,{prefix:"underscore method:",logMethod:console.log}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment