Eval Vs If Statements (many If Statements)
This fiddle pretty much explains what I'm looking for. I'm trying to find the simplest way to go about coding something WITHOUT using eval. I can do it without eval but I think I
Solution 1:
Another approach would be to re-write the current usage of accessing object properties via the dot notation
and use the bracket notation
instead.
launch_no_eval:function(id) {
//we init a blank dhtmlxwindow and do some other things here, then...if (id==="window_a") var x = wins.a({x:1}); //fill dhtmlxwindow with proper contentelseif (id==="window_b") var x = wins.b({x:1}); //fill dhtmlxwindow with proper contentelseif (id==="window_c") var x = wins.c({x:1}); //fill dhtmlxwindow with proper content//and so on for literally thousands of items.
}
could be re-written as follows,
launch_no_eval:function(id) {
var x = wins[id]({x:1});
}
This would mean your HTML markup can change from,
<ahref="javascript:core.launch_no_eval('window_a');">Window-A</a><br><ahref="javascript:core.launch_no_eval('window_b');">Window-B</a><br><ahref="javascript:core.launch_no_eval('window_c');">Window-C</a><br><br>
to,
<ahref="javascript:core.launch_no_eval('a');">Window-A</a><br><ahref="javascript:core.launch_no_eval('b');">Window-B</a><br><ahref="javascript:core.launch_no_eval('c');">Window-C</a><br><br>
As Daniel commented below, assuming you have no control over the HTML markup and must use the window_#
value being passed in then you could perform the following,
launch_no_eval:function(id) {
// replace 'window_' with empty string to retrieve unique idvar x = wins[id.replace('window_', '')]({x:1});
}
Post a Comment for "Eval Vs If Statements (many If Statements)"