有没有办法用一个函数包装所有 JavaScript 方法?

发布于 2024-09-27 04:23:33 字数 327 浏览 2 评论 0原文

我想用一些日志代码包装每个函数调用。会产生如下输出的东西:

func1(param1, param2)
func2(param1)
func3()
func4(param1, param2)

理想情况下,我想要一个以下形式的 API:

function globalBefore(func);
function globalAfter(func);

我已经为此搜索了很多,但似乎只有面向方面的解决方案需要您包装要记录的特定函数,或者其他什么。我想要一些适用于全局范围内的每个函数的东西(显然除了它本身)。

I want to wrap every function call with some logging code. Something that would produce output like:

func1(param1, param2)
func2(param1)
func3()
func4(param1, param2)

Ideally, I would like an API of the form:

function globalBefore(func);
function globalAfter(func);

I've googled quite a bit for this, but it seems like there's only aspect-oriented solutions that require you to wrap the specific functions you want to log, or whatever. I want something that applies to every function in the global scope (except itself, obviously).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-10-04 04:23:33

一个简单的方法就像这样

var functionPool = {} // create a variable to hold the original versions of the functions

for( var func in window ) // scan all items in window scope
{
  if (typeof(window[func]) === 'function') // if item is a function
  {
    functionPool[func] = window[func]; // store the original to our global pool
    (function(){ // create an closure to maintain function name
         var functionName = func;
         window[functionName] = function(){ // overwrite the function with our own version
         var args = [].splice.call(arguments,0); // convert arguments to array
         // do the logging before callling the method
         console.log('logging: ' + functionName + '('+args.join(',')+')');
         // call the original method but in the window scope, and return the results
         return functionPool[functionName].apply(window, args );
         // additional logging could take place here if we stored the return value ..
        }
      })();
  }
}

要撤消,您需要运行

for (func in functionPool)
  window[func] = functionPool[func];

Notes
这仅处理全局函数,但您可以轻松扩展它以处理特定对象或方法等。

A simple approach would be something like this

var functionPool = {} // create a variable to hold the original versions of the functions

for( var func in window ) // scan all items in window scope
{
  if (typeof(window[func]) === 'function') // if item is a function
  {
    functionPool[func] = window[func]; // store the original to our global pool
    (function(){ // create an closure to maintain function name
         var functionName = func;
         window[functionName] = function(){ // overwrite the function with our own version
         var args = [].splice.call(arguments,0); // convert arguments to array
         // do the logging before callling the method
         console.log('logging: ' + functionName + '('+args.join(',')+')');
         // call the original method but in the window scope, and return the results
         return functionPool[functionName].apply(window, args );
         // additional logging could take place here if we stored the return value ..
        }
      })();
  }
}

To undo you would need to run the

for (func in functionPool)
  window[func] = functionPool[func];

Notes
This handles only global functions, but you can easily extend it to handle specific objects or methods etc..

与之呼应 2024-10-04 04:23:33

jquery-aop 可能可以解决问题?

jquery-aop might do the trick?

嘴硬脾气大 2024-10-04 04:23:33

也许您可以有一个函数,将要执行的函数作为参数传递给该函数:

function runner(func_to_run) {
    alert('about to run ' + func_to_run.name);

    func_to_run();

}

function test() {
    alert ('in test');
}

runner(test)

Maybe you could have a function to which you pass the function to execute as a parameter:

function runner(func_to_run) {
    alert('about to run ' + func_to_run.name);

    func_to_run();

}

function test() {
    alert ('in test');
}

runner(test)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文