Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery logger plugin
    primarykey
    data
    text
    <p>I'm working on a jQuery plugin that allows you to log any javascript class or object. The idea is to override each function inside the object, or prototype of a function.</p> <pre><code>(function($) { "use strict"; $.log = function(object, logger) { if (!$.isFunction(logger)) { logger = function(name, args) { console.log(name + "(" + $.makeArray(args).join(", ") + ")"); }; } var s = $.isFunction(object) ? object.prototype : object; for (name in s) { var fn = s[name]; if ($.isFunction(fn)) { s[name] = (function(name, fn) { return function() { logger(name, arguments); return fn.apply(this, arguments); }; })(name, fn); } } }; })(jQuery); </code></pre> <p>This seems to work for logging individual plugins. For example <code>$.log($.ui.tabs);</code> logs all the function calls inside the tabs prototype.</p> <p>But when I want to log all of jQuery <code>$.log($);</code> it's giving me some reference error. I can't figure out why I'm getting this error. I'm under the impression it has something to do with either <code>this</code> or the arguments being passed, but I'm not sure.</p> <p>Edit: Now I think about It some more it might also be caused because the overridden function always returns.</p> <p>I created a fiddle to demo the problem: <a href="http://jsfiddle.net/Sj6xN/4/" rel="nofollow">http://jsfiddle.net/Sj6xN/4/</a></p> <p><strong>EDIT:</strong></p> <p>This is the code i ended up with, so far working perfectly:</p> <pre><code>(function($) { "use strict"; var Logger = function(options) { this.options = $.extend(this.defaults, options); }; Logger.prototype = { defaults: { inherited: false, deep: false, logWriter: function(name, args) { console.log("CALL: " + name + "(" + $.makeArray(args).join(", ") + ")"); } }, augment: function(object) { var self = this; // Make sure this object is not already augmented if (object.__isAugmented__) { return; } // Set 'isAugmented' to prevent recursion object.__isAugmented__ = true; // Loop through the object for (var name in object) { var originalFunction = object[name]; // If it's a function and the function is not inherited or 'inherited' is enabled augment it if ($.isFunction(originalFunction) &amp;&amp; (object.hasOwnProperty(name) || self.options.inherited)) { // Wrap in self executing function so references to 'name' and 'orginalFunction' are maintained object[name] = (function(name, originalFunction) { // If the function has a prototype and 'deep' is enabled augment that as well if (self.options.deep &amp;&amp; originalFunction.prototype) { self.augment(originalFunction.prototype); } var augmentedFunction = function() { // Execute log writer self.options.logWriter(name, arguments); // Call original function return originalFunction.apply(this, arguments); }; // Inherit prototype of original function augmentedFunction.prototype = originalFunction.prototype; // Return the augmented function return augmentedFunction; })(name, originalFunction); } // If it's a plain object and 'deep' is enabled augment that as well else if (self.options.deep &amp;&amp; $.isPlainObject(originalFunction)) { self.augment(originalFunction); } } } }; $.log = function(object, options) { var logger = new Logger(options); // If the object is a function use it's prototype, otherwise assume a plain object object = $.isFunction(object) ? object.prototype : object; // Augment logger.augment(object); }; })(jQuery); </code></pre> <p>Can be used like this:</p> <pre><code>$.log(&lt;object or function&gt; [, { inherited: &lt;bool&gt;, deep: &lt;bool&gt;, logWriter: &lt;function(name, args)&gt; }]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload