计算对 eval 的调用次数
我想计算我们的 javascript 应用程序中对 eval 的调用次数。
我想出了以下内容,但它会产生错误。这些错误很难追踪,而且我对应用程序的了解有限。
你能告诉我我的代码有什么问题吗?
increment = function (){
var me = arguments.callee;
if (!me.count) me.count = 0;
return ++me.count;
}
var oldEval = eval;
eval = function eval(string){
console.log('eval number ', increment());
return oldEval(string);
}
或者您知道计算 eval 使用情况的替代方法吗?
谢谢奥利维尔
I would like to count the number of calls made to eval in our javascript application.
I came up with the following, but it generates errors. These errors are hard to track, and my knowledge of the app is limited.
Can you tell what is wrong with my code ?
increment = function (){
var me = arguments.callee;
if (!me.count) me.count = 0;
return ++me.count;
}
var oldEval = eval;
eval = function eval(string){
console.log('eval number ', increment());
return oldEval(string);
}
Or to you know an alternative way to count the use of eval ?
thanks
Olivier
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,没有一种无懈可击的方法来模拟/包装 eval,因为它不是一个正常的函数。它可以访问(读和写)直接调用者的局部变量。
这是你无法通过原生 JavaScript 函数模拟的魔力。您无法从 myWrappedEval 获取调用者中的
a
值或写回更改后的值。虽然您可以使用arguments.caller
获取调用者函数,但无法读取函数调用的局部变量。您必须在源代码中找到的每次使用
eval
之前手动插入对increment
的调用。或者,更好的做法是,删除对 eval 的所有调用,并用更好的代码替换它,使调用总数为零。 ;-)Unfortunately there is not a watertight way of emulating/wrapping eval, because it isn't a normal function. It can access (both read and write) the local variables of the direct caller.
This is magic you can't emulate from a native JavaScript function. You'd have no way from myWrappedEval to get the value of
a
in the caller or write back the changed value. Whilst you can get hold of the caller function usingarguments.caller
, there's no way to read the locals of the function invocation.You'll have to manually insert a call to
increment
just before each usage ofeval
you can find in the source. Or, better, remove every call toeval
and replace it with better code, making the call total zero. ;-)我想出了一个基本的解决方案。它似乎有效,至少我可以收集一些数据,但我不能 100% 确定它是正确的。
所以我只是这样做:
这样,firebug 的分析器就能够计算对 eval 的调用,但不会对计数进行分组,因此我必须复制并粘贴到Excel,并构建一个数据透视表才能获取数据。
I came up with a rudimentary solution. It seems to work, at least I can gather some data, but I"m not 100% sure it is correct.
so I just do :
With that, firebug's profiler is able to count the calls to eval, but does not group that counting, therefore I have to copy and paste to excel, and build a pivot Table in order to get the data.
唯一跳出来的是你的参数名称“string”
尝试将其更改为“str”或其他名称。有些浏览器可能不喜欢“String”,因为它实际上是一个类名(尤其是有时经常忽略大小写的 IE)
注意我测试了您的代码,它可以在 Firefox 3.5(安装了 Firebug 插件)中工作
如果您不这样做定义了
console
对象,或者在所述控制台对象上定义了log
方法 - 您将收到错误。 (例如,如果未安装 Firebug 或类似软件)The only thing that jumps out is your argument name "string"
Try changing it to "str" or something. Some browsers might not like "String" since it is actually a Class name (esp. IE that often ignores case sensitivity sometimes)
Note I tested your code, and it works in Firefox 3.5 (with the Firebug addon installed)
If you don't have a
console
object defined, or alog
method on said console object - you will get errors. (e.g. if Firebug or similar is not installed)尝试这个修改后的版本
string
重命名为str
console.log
(并非在所有浏览器中都可用)Try this modified version
string
tostr
console.log
(which isn't available in all browser)