2017-07-06 35 views
0

比方说,我有那个做事功能了一把:优雅切换许多功能

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

然后我把所有的“做“在一个对象的功能,所以我可以动态地设置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' 

是否有WA y在遍历所有不匹配的stop函数的同时启用一个do函数?

例如,如果我这样做var what = 'this'; doing[what]();,我希望它不仅doThis();stopStuff(); stopThings(); stopIt(); stopThat();

我无法想出一个巧妙的方法,不涉及冗长的if语句或case /开关。

+0

我会做一个功能,像stopDoing(noThis),如果这个功能具有noThis参数集,它会调用每个停止功能,而不是在参数之一。如果没有设置参数,它会调用所有停止功能......类似的东西! –

+0

如何停止所有功能,然后开始所需功能。 – gusaindpk

+0

几乎肯定是一个XY问题。你介意分享你为什么需要这样的原因吗?这些事情是从什么开始和停止的,什么决定了他们是否需要开始或停止? – Amadan

回答

3

如果你所有的DO /停止功能是全球性的 - 本作品

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

+0

谢谢。这对我来说最有意义,并且对我的使用情况非常合适! ES6的奖励积分(在删除之前)。 – daveycroqet

+1

我可以把ES6回 –

+0

@daveycroqet - 我已经把ES6回到第二版(我认为其实更普遍的是) - 加'.sort'停止所有,然后在很多方面开始选择比我更好 –

1

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

如果你所有的DO /停止功能是全球 - 这工作

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](); 
    } 
} 
1

让函数需要一个参数,并通过功能迭代一个数组是除了参数之外,调用它们每一个。例如:

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

var stopping = [ 
    stopStuff: function() { stoppingStuff() }, 
    ... 
]; 
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: doStuff, 
    things: doThings, 
    it:  doIt, 
    this: doThis, 
    that: doThat 
}; 

var stopping = { 
    stuff: stopStuff, 
    things: stopThings, 
    it:  stopIt, 
    this: stopThis, 
    that: stopThat 
}; 

var a = Object.keys(stopping); 
a.splice(a.indexOf(what), 1); 
Object.keys(stopping).map(function(key) { 
    return stopping[key](); 
}); 
doing[what](); 
+0

- 尽管'东西:函数(){doStuff();}'可以'东西:doStuff'为赢:p –

+0

我的答案:)谢谢 – Vineesh

+0

无论你的答案和Jaromanda的配合更新非常好听。我真诚地希望我能给你两个信用。我不知道谁更有效率。无论如何,谢谢! – daveycroqet

1

双向,您可以在这取决于运行的额外功能的成本做。

  1. 在开始新的do函数之前调用所有stop函数。
  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](); 
    
+0

@JaromandaX不,我已经更新小提琴工作正常。 https://jsfiddle.net/8xjy8gax/1/ – gusaindpk

+0

我的意思是代码看起来不像代码块:p一切正常 –