jQuery logger插件

我正在开发一个jQuery插件,允许你记录任何javascript类或对象。 我们的想法是覆盖对象内部的每个函数或函数的原型。

(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); 

这似乎适用于记录单个插件。 例如$.log($.ui.tabs); 记录选项卡原型中的所有函数调用。

但是当我想记录所有jQuery $.log($); 它给了我一些参考错误。 我无法弄清楚为什么我会收到这个错误。 我的印象是它与this或被传递的论点有关,但我不确定。

编辑:现在我考虑更多它也可能是因为被覆盖的函数总是返回。

我创建了一个演示问题的小提琴: http : //jsfiddle.net/Sj6xN/4/

编辑:

这是我最终得到的代码,到目前为止工作完美:

 (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) && (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 && 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 && $.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); 

可以像这样使用:

 $.log( [, { inherited: , deep: , logWriter:  }]); 

仔细看看错误。

 Uncaught ReferenceError: name is not defined 

意味着您没有定义名称,并且由于您处于严格模式 ,因此您无法在未定义变量的情况下使用变量(通常,如果您这样做,它将是全局变量,但不是严格模式)。 因此,如果您在它之前编写一个var name ,则不会再出现此错误。

虽然没有tab方法还有另一个错误。 另一个错误说tabs不是对象的一个​​方法,因为当你包装函数时,你没有inheritance原型,所以当用new调用函数时,它没有原型函数( tabs是其中之一)他们)。

这是固定代码: http : //jsfiddle.net/Sj6xN/8/