1

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

2 Answers 2

3

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);
1
  • thanks, your right, replaced all evals with this type of dynamic execution, awesome.
    – LeonS
    Commented Apr 14, 2011 at 12:48
0
this.find(element_to_append_the_controller.toString())[controller_to_load](options_for_controller)

AKA don't use eval.

Not the answer you're looking for? Browse other questions tagged or ask your own question.