我想删除冗余代码并转入单独的Jquery函数

时间:2017-06-08 10:24:26

标签: javascript jquery

这是我试图删除冗余代码并将代码移动到单独函数的代码。

def var1 = 'bin'
sh """
   echo var1 = $var1
   shell_var = 'yyy'
   echo shell_var = \$shell_var
""""

但是我在将变量和缓存选择器传递给其他函数时遇到了问题。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我不知道你的实现是如何完成的,但我真的认为你可以改进它,顺便说一下,你可以用两种不同的方式减少你的代码:

var myFunction = function(option, filter, array, selector, subType) {
    if(option && option.val()){
        var optList = option.val();
        var optLength = optList.length;
        //Condition to check empty input type
        if( optLength > 0) {
            var l = 0;
            for(var j = 0; j < optLength; j++) {
                if( ($.inArray( selector.select2('data')[j].text, filterList)) == -1 ){
                    filter.push(selector.select2('data')[j].text);
                    if(optList[j].contains('_subgroup')){
                        var res = optList[j].split("_");
                        subType[l]=res[0];
                        l=l+1;
                    } else {
                        array.push(optList[j]);
                    }
                }
            }
        }
    }
}

致电:myFunction(this.$countryOptions, this.filter, this.aGeography, $('#countryOptions'), this.aSubgeotype)

// data = {option, filter, array, selector, subType}
var myFunction = function(data) {
    if(data.option && data.option.val()){
        var optList = data.option.val();
        var optLength = optList.length;
        //Condition to check empty input type
        if( optLength > 0) {
            var l = 0;
            for(var j = 0; j < optLength; j++) {
                if( ($.inArray( data.selector.select2('data')[j].text, filterList)) == -1 ){
                    data.filter.push(data.selector.select2('data')[j].text);
                    if(optList[j].contains('_subgroup')){
                        var res = optList[j].split("_");
                        data.subType[l]=res[0];
                        l=l+1;
                    } else {
                        data.array.push(optList[j]);
                    }
                }
            }
        }
    }
}

致电:

myFunction({
    option: this.$countryOptions,
    filter: this.filter,
    array: this.aGeography,
    selector: $('#countryOptions'),
    subType: this.aSubgeotype
});

var data = {
    option: this.$countryOptions,
    filter: this.filter,
    array: this.aGeography,
    selector: $('#countryOptions'),
    subType: this.aSubgeotype
}
myFunction(data);

第一种方法是逐个传递数据,第二种方法是将数据传递给json对象。