Javascript eval和googleclosure编译器存在问题
我正在使用google closure压缩我的代码,但以下代码行有问题:Javascript eval和googleclosure编译器存在问题,javascript,eval,google-closure-compiler,Javascript,Eval,Google Closure Compiler,我正在使用google closure压缩我的代码,但以下代码行有问题: eval('this.find(\''+ element_to_append_the_controller+ '\').'+controller_to_load+'(options_for_controller)'); 我必须使用eval,因为我必须在元素上执行的方法(controller\u to\u load)是可变的,并且取决于我得到的参数 我的问题是,我必须将一个对象传递给该方法,因此我将其作为变
eval('this.find(\''+ element_to_append_the_controller+ '\').'+controller_to_load+'(options_for_controller)');
我必须使用eval,因为我必须在元素上执行的方法(controller\u to\u load
)是可变的,并且取决于我得到的参数
我的问题是,我必须将一个对象传递给该方法,因此我将其作为变量名的字符串表示(options\u for\u controller
),但是闭包将更改该名称,并且不会更改我的eval字符串中的变量名
我的解决办法是:
- 将变量名动态获取为字符串
- 正在将对象(使用回调函数)解析为字符串
- 禁用压缩这些代码行
AKA不使用eval。一些程序员使用eval是因为他们没有意识到可以编写
eval('a.+b)
而不是编写a[b]
试试这个而不是你的评估()
谢谢,你说的对,用这种动态执行取代了所有的eval,太棒了。
this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller)
this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller);