eval 和 google 闭包编译器的问题
我正在使用 google 闭包来压缩我的代码,但以下代码行有问题:
eval('this.find(\''+ element_to_append_the_controller+ '\').'+controller_to_load+'(options_for_controller)');
我必须使用 eval,因为我必须在元素上执行的方法 (controller_to_load
) 是可变的,并且取决于我得到的参数。
我的问题是我必须将一个对象传递给该方法,因此我将其作为变量名称的字符串表示形式(options_for_controller
),但是闭包将更改该名称并且不会更改我的 eval 字符串中的变量名称。
我的解决方案是:
- 将变量名动态作为字符串
- 解析对象(带有回调函数)到字符串
- 禁用这些代码行的压缩
但是我该如何做其中之一或者还有其他解决方案?
谢谢
I'm using google closure to compress my code but I have a problem with the following line of code:
eval('this.find(\''+ element_to_append_the_controller+ '\').'+controller_to_load+'(options_for_controller)');
I have to use eval because the method (controller_to_load
) I have to execute on the element is variable and depend on the params I get.
My Problem is that I have to pass an object to that method, so I'm doing that as an String representation of the variable name(options_for_controller
), but closure will change that name and won't change the variable name in my eval string.
My solutions would be:
- getting the variable name dynamic as string
- parsing object (with callback functions) to string
- disable compressing for these line of codes
But how can I do one of them or is there another solution?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些程序员使用 eval 是因为他们没有意识到除了编写
eval('a.' + b)
之外,您还可以编写a[b]
试试这个而不是 eval( )
Some programmers use eval because they don't realise instead of writing
eval('a.' + b)
you can writea[b]
Try this instead of your eval()
又名不使用 eval。
AKA don't use eval.