Alternatives to "eval"
Join the Discussion
Once you realise that all global variables are held in the window array it becomes rather obvious that you can refer to that same field name without needing to use eval by referring to it like this:
This second way of referencing the same field is as simple to code and way more efficient than using eval.
We now look at coding it as:
This code may be fractionally longer than using eval but it makes it much clearer to someone reading the code as to exactly what it is that the code is trying to do. Browsers should also have a clearer idea of what you want when you code it this way so they should produce faster running code as well.
It is only when the code that you want to "evaluate" itself contains functions that the code required to avoid using eval becomes more complicated.