eval 和 google 闭包编译器的问题

发布于 2024-11-01 03:00:28 字数 528 浏览 8 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不顾 2024-11-08 03:00:28

一些程序员使用 eval 是因为他们没有意识到除了编写 eval('a.' + b) 之外,您还可以编写 a[b]

试试这个而不是 eval( )

this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller);

Some programmers use eval because they don't realise instead of writing eval('a.' + b) you can write a[b]

Try this instead of your eval()

this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller);
好久不见√ 2024-11-08 03:00:28
this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller)

又名不使用 eval。

this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller)

AKA don't use eval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文