1

我正在使用 google 闭包来压缩我的代码,但我对以下代码行有疑问:

        eval('this.find(\''+ element_to_append_the_controller+ '\').'+controller_to_load+'(options_for_controller)');

我必须使用 eval 因为controller_to_load我必须在元素上执行的方法 () 是可变的,并且取决于我得到的参数。

我的问题是我必须将一个对象传递给该方法,因此我将其作为变量名(options_for_controller)的字符串表示形式,但闭包会更改该名称并且不会更改我的 eval 字符串中的变量名。

我的解决方案是:

  • 将变量名动态作为字符串
  • 将对象(带有回调函数)解析为字符串
  • 禁用这些代码行的压缩

但是我该怎么做呢?还是有另一种解决方案?

谢谢

4

2 回答 2

3

一些程序员使用 eval 是因为他们没有意识到eval('a.' + b)你可以写而不是写a[b]

试试这个而不是你的 eval()

this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller);
于 2011-04-14T12:32:52.617 回答
0
this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller)

AKA 不要使用 eval。

于 2011-04-14T12:32:16.417 回答