问题
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 will have to write 1000s of IF statements. Or is there another way?
http://jsfiddle.net/243rz8eq/9/
HTML
Eval way...<br>
<a href="javascript:core.launch('wins.a({x:1})');">Window-A</a><br>
<a href="javascript:core.launch('wins.b({x:1})');">Window-B</a><br>
<a href="javascript:core.launch('wins.c({x:1})');">Window-C</a><br><br>
Non-Eval way. Requires if statement for each window (there will be thousands). Or is there a simpler way I'm not seeing?<br>
<a href="javascript:core.launch_no_eval('window_a');">Window-A</a><br>
<a href="javascript:core.launch_no_eval('window_b');">Window-B</a><br>
<a href="javascript:core.launch_no_eval('window_c');">Window-C</a><br><br>
JavaScript
window.core = {
launch: function(obj_string) {
//we init a blank dhtmlxwindow and do some other things here, then...
var x = eval("new " + obj_string); //fill dhtmlxwindow with proper content
},
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 content
else if (id==="window_b") var x = wins.b({x:1}); //fill dhtmlxwindow with proper content
else if (id==="window_c") var x = wins.c({x:1}); //fill dhtmlxwindow with proper content
//and so on for literally thousands of items.
}
};
window.wins = {
a: function(args) {
//this.myName = 'wins.a'; is used for the help topic.
//DB contains columns: [item] / [helpurl]
//Example Data: [wins.a] / [/help/gettingstarted.html]
//That is why in this previous post (http://stackoverflow.com/q/28096922/3112803)
//I was wanting to know if a function could know it's own name so I wouldn't
//have to type this line below for 1000s of items.
this.myName = 'wins.a';
console.log('Window-A is now displayed. Use "'+this.myName+'" to make link to help topic.');
},
b: function(args) {
this.myName = 'wins.b';
console.log('Window-B is now displayed. Use "'+this.myName+'" to make link to help topic.');
},
c: function(args) {
this.myName = 'wins.c';
console.log('Window-C is now displayed. Use "'+this.myName+'" to make link to help topic.');
}
};
回答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 content
else if (id==="window_b") var x = wins.b({x:1}); //fill dhtmlxwindow with proper content
else if (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,
<a href="javascript:core.launch_no_eval('window_a');">Window-A</a><br>
<a href="javascript:core.launch_no_eval('window_b');">Window-B</a><br>
<a href="javascript:core.launch_no_eval('window_c');">Window-C</a><br><br>
to,
<a href="javascript:core.launch_no_eval('a');">Window-A</a><br>
<a href="javascript:core.launch_no_eval('b');">Window-B</a><br>
<a href="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 id
var x = wins[id.replace('window_', '')]({x:1});
}
来源:https://stackoverflow.com/questions/28098239/eval-vs-if-statements-many-if-statements