具有eval的功能无法通过缩小实现 [英] Function with eval won't make it through minification

查看:115
本文介绍了具有eval的功能无法通过缩小实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的怪异函数,可让我的用户创建自己的javascript代码

Here is my weird function that let my users create their own javascript code

function evalThisFunction(functionBody){        
     var func;
     eval("func = " + functionBody);
     return func;
}

但是在使用Closure Compiler( http://closure-compiler.appspot.com/ ),我得到以下结果:

But after minification with Closure Compiler (http://closure-compiler.appspot.com/), I get this result :

function a(b){eval("func = "+b);}

您是否看到一种方法,可以修改我的怪异函数,使其在缩小后仍然可以使用?

Do you see a way I can modify my weird function so it will still works after minification?

推荐答案

是的,请使用函数构造函数:

function evalThisFunction(functionBody){        
     return Function(functionBody);
}

或者,您可以将上面的代码完全换成Function,看来它还是可以做您想做的事情. eval有范围问题.

Alternatively, you can swap out the above code entirely for Function , it seems to do what you want anyway. eval has scoping issues.

这篇关于具有eval的功能无法通过缩小实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆