Eval vs IF 语句(许多 IF 语句)

Eval vs IF statements (many IF statements)

这个 fiddle 几乎可以解释我要找的东西。我试图找到最简单的方法来编写代码而不使用 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 (
        //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.');
    }
};

另一种方法是重写通过 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>

正如丹尼尔在下面评论的那样,假设您无法控制 HTML 标记并且必须使用传入的 window_# 值,那么您可以执行以下操作,

launch_no_eval:function(id) {
        // replace 'window_' with empty string to retrieve unique id
        var x = wins[id.replace('window_', '')]({x:1});
}