简体   繁体   English

jquery记录所有函数调用

[英]jquery log all function calls

I want to log all jquery calls like: 我想记录所有jquery调用,如:

Html HTML

<div id="element"></div>

Jquery jQuery的

$("#element").addClass("yourClass");
$("#element").hide();

The result should be: 结果应该是:

<div id="element" data-js="1,2"></div>

* The "data-js" attribute added by the base function to let me know in which line the original code. *由base函数添加的“data-js”属性让我知道原始代码在哪一行。

basically, I need to create base plugin in jquery or callback to each plugin that I can register it somewhere. 基本上,我需要在jquery中创建基本插件或回调每个插件,我可以在某处注册它。

I'm not aware of any jQuery method "hook" capability built into all jQuery methods so I think the only way you could know when all jQuery methods are called from your own javascript is to dynamically install a hook for every jQuery method. 我不知道所有jQuery方法中都内置了任何jQuery方法“钩子”功能,所以我认为你知道从你自己的javascript调用所有jQuery方法的唯一方法是为每个jQuery方法动态安装一个钩子。 You could loop through all the methods in jQuery.fn (after all plug-ins are installed) and install a shim that would do your logging and then call the original method being careful to preserve this , arguments and return values. 您可以循环遍历jQuery.fn所有方法(在安装所有插件之后)并安装一个可以进行日志记录的填充程序,然后调用原始方法,小心保存this参数和返回值。

The shim would look something like this: 垫片看起来像这样:

function installShims() {
    for (var method in jQuery.fn) {
        if (jQuery.fn.hasOwnProperty(method) && 
          typeof jQuery.fn[method] === "function" &&
          method !== "init") {
            (function(oldMethod) {
                 jQuery.fn[method] = function() {
                     // do your logging here
                     // you have access to this and to arguments
                     return oldMethod.apply(this, arguments)
                 };
            })(jQuery.fn[method])
        }
    }
}

Working demo: http://jsfiddle.net/jfriend00/24nvd/ 工作演示: http//jsfiddle.net/jfriend00/24nvd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM