这个小提琴几乎解释了我正在寻找的东西。我试图找到最简单的方法来编写一些不使用eval
的东西。我可以在没有eval
的情况下完成,但我认为我必须编写1000个IF语句。或者还有另一种方式吗?
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.');
}
};
答案 0 :(得分:2)
另一种方法是重新编写通过dot notation
访问object properties的当前使用情况,并使用bracket notation
代替。
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.
}
可以重写如下,
launch_no_eval:function(id) {
var x = wins[id]({x:1});
}
这意味着您的HTML标记可能会改变,
<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>
要,
<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>
正如Daniel在下面评论的那样,假设您无法控制HTML标记并且必须使用传入的window_#
值,那么您可以执行以下操作,
launch_no_eval:function(id) {
// replace 'window_' with empty string to retrieve unique id
var x = wins[id.replace('window_', '')]({x:1});
}