I'm debugging and upgrading some legacy PHP and JavaScript code and I've found some eval() functions I cannot get rid of. Coming from desktop programming environment, with compiled languages such as C/C++ I've never used the eval() function in javascript mainly because I didn't know of its existence. I've been googling a while and from all that I've read I understand that eval() evaluates a string as if it was legit javascript code and executes it. I've managed to get rid of eval() functions which were being used to access dynamic properties and decoding JSON objects, however there's a fairly advanced (and obscure) class definition which uses eval() that I haven't been able to defeat.
Sample code:
function Ddl(N) {
this.lyr = document.getElementById(N);
this.obj = N+"DDL_Lyr";
eval(this.obj+"=this");
this.lyr.setAttribute("onchange", this.obj+".onChange();");
}
This function is an object constructor. DDL objects are used in the web page as:
ddl_name = new Ddl('id_of_select');
ddl_name.onChange = event_function;
For all that I know, eval(this.obj + "=this")
should be the same as this.obj = this;
but then the onchange event function event_function
is not fired. I haven't been able to guess why using firebug. If I use this.obj = this
I can see the value of this.obj changing from id_of_selectDDL_Lyr
to a pointer to the DDL object itself so I can figure why setAttribute fails, but using the eval() function this.obj
doesn't look to reflect the changes as code is executed, it always remains as 'id_of_selectDDL_Lyr', however it's doing something since if I comment it out the onchange event is not fired.
I suspect it may be related to local or global scope, since I've read that eval() calls are executed in a different scope, but I don't know how to ckeck it nor I know how to replace eval() with normal javascript code.