优雅地切换许多功能

时间:2017-07-06 04:09:23

标签: javascript jquery function object

假设我有一些执行操作的函数:

function doStuff()  { console.log('doing stuff'); }
function doThings() { console.log('doing things'); }
function doIt()     { console.log('doing it'); }
function doThis()   { console.log('doing this'); }
function doThat()   { console.log('doing that'); }

然后同样多的人禁用他们的行为:

function stopStuff()  { console.log('stopping stuff'); }
function stopThings() { console.log('stopping things'); }
function stopIt()     { console.log('stopping it'); }
function stopThis()   { console.log('stopping this'); }
function stopThat()   { console.log('stopping that'); }

然后我将所有'do'函数放在一个对象中,这样我就可以通过设置what来动态访问它们:

var what = 'things';

var doing = {
    stuff:  function() { doStuff(); },
    things: function() { doThings(); },
    it:     function() { doIt(); },
    this:   function() { doThis(); },
    that:   function() { doThat(); }
};

doing[what](); //console logs 'doing things'

在迭代所有不匹配的do函数时,有没有办法启用一个stop函数?

例如,如果我var what = 'this'; doing[what]();,我不仅要doThis();而且要stopStuff(); stopThings(); stopIt(); stopThat();

我无法想出一个优雅的方法,不需要冗长的if语句或case / switch。

5 个答案:

答案 0 :(得分:3)

如果您的所有执行/停止功能都是全局的 - 这可行



function doStuff()  { console.log('doing stuff'); }
function doThings() { console.log('doing things'); }
function doIt()     { console.log('doing it'); }
function doThis()   { console.log('doing this'); }
function doThat()   { console.log('doing that'); }

function stopStuff()  { console.log('stopping stuff'); };
function stopThings() { console.log('stopping things'); };
function stopIt()     { console.log('stopping it'); };
function stopThis()   { console.log('stopping this'); };
function stopThat()   { console.log('stopping that'); };


var run = function(root) {
    var fnNames = ['stuff', 'things', 'it', 'this', 'that'];
    return function(what) {
        fnNames.forEach(function (fn) {
            var fnName = fn[0].toUpperCase() + fn.slice(1);
            root[(what == fn ? 'do' : 'stop') + fnName]();
        });
    };
}(window);
// usage
console.log('things');
run('things');
console.log('this');
run('this');




然而,如果它们不是全球性的,那有点麻烦,但不是很多



function doStuff()  { console.log('doing stuff'); }
function doThings() { console.log('doing things'); }
function doIt()     { console.log('doing it'); }
function doThis()   { console.log('doing this'); }
function doThat()   { console.log('doing that'); }

function stopStuff()  { console.log('stopping stuff'); }
function stopThings() { console.log('stopping things'); }
function stopIt()     { console.log('stopping it'); }
function stopThis()   { console.log('stopping this'); }
function stopThat()   { console.log('stopping that'); }

var run = (() => {
    var fns = {
        stuff:  { run: doStuff,  stop: stopStuff  },
        things: { run: doThings, stop: stopThings },
        it:     { run: doIt,     stop: stopIt     },
        "this": { run: doThis,   stop: stopThis   },
        that:   { run: doThat,   stop: stopThat   }
    };
    return what => Object.keys(fns)
        // include the sort only if you need to stop all first before start
        // change a == what to b == what to start selected and then stop the rest
        .sort((a,b) => a == what) 
        .forEach(key => fns[key][what == key ? 'run' : 'stop']());
})();

console.log('things');
run('things');
console.log('this');
run('this');




答案 1 :(得分:2)

.columns

答案 2 :(得分:1)

您可以使用简单的for循环,如下面的代码

  

如果您的所有执行/停止功能都是全局的 - 这可行

for(var idx in doing) {
    if(idx == what) {
        doing[idx]();
    }
    else {
       var fn = idx.charAt(0).toUpperCase() + idx.slice(1);
       var fname = "stop"+fn;
       window[fname]();
    }
}

答案 3 :(得分:1)

让函数接受一个参数,并遍历调用除参数之外的每个函数的函数数组。示例:

var doing = [
  stuff:  function(func) { 
    stopping.forEach(element => {
      if (element !== func) {
        element()
      }
    }
    doStuff()
  },
  ...
];

var stopping = [
  stopStuff:  function() { stoppingStuff() },
  ...
];

答案 4 :(得分:1)

根据运行额外的功能成本,你可以采用两种方式。

  1. 在开始新的do功能之前调用所有停止功能。
  2. 或者过滤特定的停止功能。

                function doStuff() {
                console.log('doing stuff');
            }
    
            function doThings() {
                console.log('doing things');
            }
    
            function doIt() {
                console.log('doing it');
            }
    
            function doThis() {
                console.log('doing this');
            }
    
            function doThat() {
                console.log('doing that');
            }
    
            function stopStuff() {
                console.log('stopping stuff');
            }
    
            function stopThings() {
                console.log('stopping things');
            }
    
            function stopIt() {
                console.log('stopping it');
            }
    
            function stopThis() {
                console.log('stopping this');
            }
    
            function stopThat() {
                console.log('stopping that');
            }
    
            var what = 'things';
    
            var doing = {
                stuff: function() {
                    doStuff();
                },
                things: function() {
                    doThings();
                },
                it: function() {
                    doIt();
                },
                this: function() {
                    doThis();
                },
                that: function() {
                    doThat();
                }
            };
    
            var stopping = {
                stuff: function() {
                    stopStuff();
                },
                things: function() {
                    stopThings();
                },
                it: function() {
                    stopIt();
                },
                this: function() {
                    stopThis();
                },
                that: function() {
                    stopThat();
                }
            };
    
            var stopKeys = Object.keys(stopping);
    
            function stopsOthers(doing) {
                arr = stopKeys.filter(function(item) {
                    return item !== doing;
                });
                arr.forEach(function(key) {
                    stopping[key]();
                });
            }
            stopsOthers(what);
            doing[what]();