Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “替换模式”;如有其他",;陈述_Javascript_Design Patterns - Fatal编程技术网

Javascript “替换模式”;如有其他",;陈述

Javascript “替换模式”;如有其他",;陈述,javascript,design-patterns,Javascript,Design Patterns,我编写了以下代码: var json = {}; var test = { run: function(json) { var choice = (function(){ if (json.a != '' && json.b == '' && json.c == ''){ return 'a'} else if (json.a == '' && json.b != '

我编写了以下代码:

var json = {};
var test = {
    run: function(json)
    {
        var choice = (function(){

            if (json.a != '' && json.b == '' && json.c == ''){ return 'a'} else
            if (json.a == '' && json.b != '' && json.c == ''){ return 'b'} else
            if (json.a == '' && json.b == '' && json.c != ''){ return 'c'} else
            if (json.a != '' && json.b != '' && json.c == ''){ return 'd'} else
            if (json.a != '' && json.b == '' && json.c != ''){ return 'e'} else
            if (json.a == '' && json.b != '' && json.c != ''){ return 'f'} else
                return 'g';
        })();

        switch (choice)
        {
            case 'a': console.log('a');
                break;

            case 'b': console.log('b');
                break;

            case 'c': console.log('c');
                break;

            case 'd': console.log('d');
                break;

            case 'e': console.log('e');
                break;

            case 'f': console.log('f');
                break;

            case 'g': console.log('no arguments');
        }
    }
};

json.a = 'xxx';
json.b = '';
json.c = 'yyy';

test.run(json);
这将返回“e”,但每次json都可能不同

当每个“if”-语句都有很多代码行时,这种代码结构更易于阅读

想象一下:

if(statement){

//100 lines of code

}else
    if(statement){

    //100 lines of code
    }else ........
    //and so on.
我想知道有没有比这个解决方案更好的设计模式?
我将非常感谢您的帮助。

那么,您将如何捕获用户的输入?如果这些属性是彼此独占的,那么您最有可能使用单选按钮作为捕获输入的方式。一次只能选择一个单选按钮。那么就不需要检查所有三个属性。只需检查它们中的每一个,无论哪一个有值,都返回它

var choice = (function(){
  if (json.a != '') { return 'a'; }
  if (json.b != '') { return 'b'; }
  if (json.c != '') { return 'c'; }
  return 'd';
}

可以使用多种数组和对象方法

比如说

var res= [json.a,json.b,json.c,json.d].filter(function(str){
     return str.length;
});

var output= res.length==1 ? res[0] : "Bad args";
看到您正在使用的JSON会有更多帮助


方法更简单。在
for in
循环中通过
json
,如果有任何输入,则将该键设置为
输出。如果之后还有另一个输入,则作为“坏参数”返回。如果测试完成,则返回
输出

var json = {};

var test = {

    run: function(json)
    {
        var output = null;
        for (var s in json) {
            if (json[s]) {
                if (output) {
                    return "Bad arguments.";
                } else {
                    output = s;   
                }
            }
        }
        return output;
    }
} 

json.a='xxx';   
json.b='';
json.c='';

console.log(test.run(json));
我找到了解决方案:

var json = {};

var test = {

run: function(json)
{

    var output='';
    var func =[];
    for (var s in json) {
        if (json[s]) {
            output +=s;
        }
    }


    func['a'] = function()
    {
        return console.log('a');
    }

    func['b'] = function()
    {
        return console.log('b');
    }

    func['c'] = function()
    {
        return console.log('c');
    }

    func['ab'] = function()
    {
        return console.log('ab')
    }

    func['ac'] = function()
    {
        return console.log('ac')
    }       

    func['bc'] = function()
    {
        return console.log('bc')
    }

    func[''] = function()
    {
        return console.log('bad arguments')
    }

    func[output]();


}
}

json.a='xxx';
json.b='';
json.c='yyy';

test.run(json);

如果json对象的多个属性不为null,也应触发“坏参数”。例如,如果json.a='xxx'和json.b='xxx',则返回“坏参数”。不幸的是,可能存在所有变量(1,2,3,1和2,2和3,1和3,无)。json是简单的{a:'',b:'',c:''…}@Kris返回到示例json和更明确的过滤规则。我指出了一种处理数据的方法。。。您需要提供更多有关筛选规则的详细信息。在不了解所有标准的情况下很难创建防弹方法他基本上只想找到一种循环JSON数据的方法,并遵循以下规则:1)如果一个且只有一个属性有值,则返回该属性的键;2)如果没有属性集,或者如果有两个或更多属性集,则返回“坏参数”。他的问题是如何在不使用自己的代码行显式测试每个属性的情况下循环。我需要根据用户在json中填写的内容指定BeCharvior。如果他填写“a”和“b”,我需要运行func1(),如果他填写“a”、“b”和“c”,我需要运行func2(),如果他填写“a”,我需要运行func3()不幸的是,可能存在所有变量(1、2、3、1和2、2和3、1和3,无)。现在,您的代码只接受设置的一个值。这是您的意图吗?不,这只是一个示例,我可以添加下一个“if语句”,比如if(json.a!=''&&json.b!=''&&json.c=''){return'f'}或者。。。。。然后在开关中添加“f”字样