Created
October 12, 2016 21:34
-
-
Save jaburns/edc03ffc7a01b39aca566b91bbb66d6c to your computer and use it in GitHub Desktop.
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
| /// | |
| /// Adds logging to all functions in a JS object. | |
| /// | |
| /// Backbone usage example: | |
| /// return Backbone.View.extend({ ... }); | |
| /// becomes: | |
| /// return Backbone.View.extend(addLogs({ ... })); | |
| /// and all method invocations are now logged to the console. | |
| /// | |
| function addLogs(obj) { | |
| for (var k in obj) { | |
| if (typeof obj[k] === 'function') { | |
| (function(func) { | |
| obj[k] = function() { | |
| var args = Array.prototype.slice.call(arguments) | |
| console.log("Calling '"+func.name+"' with args ", args); | |
| func.apply(this, args); | |
| }; | |
| })(obj[k]); | |
| } | |
| } | |
| return obj; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment