Note that there are some explanatory texts on larger screens.

plurals
  1. POeval() and global scope
    primarykey
    data
    text
    <p>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.</p> <p>Sample code:</p> <pre><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();"); } </code></pre> <p>This function is an object constructor. DDL objects are used in the web page as:</p> <pre><code> ddl_name = new Ddl('id_of_select'); ddl_name.onChange = event_function; </code></pre> <p>For all that I know, <code>eval(this.obj + "=this")</code> should be the same as <code>this.obj = this;</code> but then the onchange event function <code>event_function</code> is not fired. I haven't been able to guess why using firebug. If I use <code>this.obj = this</code> I can see the value of this.obj changing from <code>id_of_selectDDL_Lyr</code> to a pointer to the DDL object itself so I can figure why setAttribute fails, but using the eval() function <code>this.obj</code> doesn't look to reflect the changes as code is executed, it always remains as 'id_of_selectDDL_Lyr', however it's doing <i>something</i> since if I comment it out the onchange event is not fired. </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload