有没有办法用一个函数包装所有 JavaScript 方法?
我想用一些日志代码包装每个函数调用。会产生如下输出的东西:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的方法就像这样
要撤消,您需要运行
Notes
这仅处理全局函数,但您可以轻松扩展它以处理特定对象或方法等。
A simple approach would be something like this
To undo you would need to run the
Notes
This handles only global functions, but you can easily extend it to handle specific objects or methods etc..
jquery-aop 可能可以解决问题?
jquery-aop might do the trick?
也许您可以有一个函数,将要执行的函数作为参数传递给该函数:
Maybe you could have a function to which you pass the function to execute as a parameter: