评估vs IF语句(许多IF语句) [英] Eval vs IF statements (many IF statements)

查看:77
本文介绍了评估vs IF语句(许多IF语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个小提琴几乎解释了我要寻找的东西。我正在尝试找到一种最简单的方法,无需使用 eval 进行编码。我可以在没有 eval 的情况下做到这一点,但我认为我将不得不编写1000个IF语句。还是有其他方法?

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

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.');
    }
};


推荐答案

另一种方法是重写当前用法通过<$ c访问对象属性 $ c>点符号,并使用括号符号

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.
    }

可以如下重写,

launch_no_eval:function(id) { 
        var x = wins[id]({x:1});
}

这意味着您的HTML标记可以更改为

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>

至,

<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 _#值传入,然后您可以执行以下操作,

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});
}

这篇关于评估vs IF语句(许多IF语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆