简体   繁体   English

如果选择值为空,我可以神奇地使选择器不起作用吗?

[英]Can I magically make the selectors non-functional if the selection value is empty?

Background: I have a function that I call like this:背景:我有一个 function,我这样称呼它:

hide_modules('string1','string2');

The function is something like: function 类似于:

function hide_modules(param1,param2) {
       MM.getModules()
         .withClass(param1)
         .exceptWithClass(param2)
         .enumerate(function(module) {
           module.hide(
             // some other code
           );
         });
}

Most of the time I call the function with values as shown above.大多数时候,我使用如上所示的值调用 function。 Sometimes I do not want 'string1' to have a value and I'd like the my function to not use that first selector, effectively like this:有时我不希望 'string1' 有一个值,我希望我的 function 不使用第一个选择器,实际上是这样的:

   MM.getModules()
     // .withClass(param1)
     .exceptWithClass(param2)
     .enumerate(function(module) {
       module.hide(
         // some other code
       );
     });

I've tried just calling it with an empty string, 0, false as param1 but the end result class selection is not what I want.我试过只用一个空字符串调用它,0, false as param1 但最终结果 class 选择不是我想要的。 Sometimes I also call it with param2 empty and not wanting to have the param2 related selector used either.有时我也会在 param2 为空的情况下调用它,并且也不想使用与 param2 相关的选择器。

So the question is: Without writing a big if-then-else statement, is there some fancy way I can make those selectors non-functional (the equivalent of commenting it out like above) when the param1 and/or param2 values are not specified?所以问题是:如果不写一个大的 if-then-else 语句,当 param1 和/或 param2 值未指定时,是否有一些奇特的方法可以使这些选择器不起作用(相当于像上面那样将其注释掉) ?


The supporting code that my function calls is provided for me in a 3rd party library that I can't change.我的 function 调用的支持代码是在我无法更改的第 3 方库中为我提供的。 I include some of the relevant parts here as it may help with the answer:我在这里包括了一些相关部分,因为它可能有助于回答:

var withClass = function (className) {
    return modulesByClass(className, true);
};
var modulesByClass = function (className, include) {
    var searchClasses = className;
    if (typeof className === "string") {
        searchClasses = className.split(" ");
    }

    var newModules = modules.filter(function (module) {
        var classes = module.data.classes.toLowerCase().split(" ");

        for (var c in searchClasses) {
            var searchClass = searchClasses[c];
            if (classes.indexOf(searchClass.toLowerCase()) !== -1) {
                return include;
            }
        }

        return !include;
    });

Since js doesn't supports function overloading, the only way is to validate your parameters inside your method.由于 js 不支持 function 重载,唯一的方法是在方法中验证参数。 Check for truthy and ternary operator will do the trick检查truthy和三元运算符就可以了

      var modules = MM.getModules();
      modules = param1 ? modules.withClass(param1) : modules;
      modules = param2 ? modules.exceptWithClass(param2) : modules;
      modules.enumerate(function(module) {
      module.hide(
         // some other code
       );
     });

to skip first parameter跳过第一个参数

hide_modules(null,'string2');

to skip second parameter跳过第二个参数

hide_modules('string1');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM