Function with eval won't make it through minification

…衆ロ難τιáo~ 提交于 2019-12-13 04:32:58

问题


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

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

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?


回答1:


Yes, use the Function constructor:

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

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



来源:https://stackoverflow.com/questions/22699745/function-with-eval-wont-make-it-through-minification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!