比方说,我有那个做事功能了一把:优雅切换许多功能
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 /开关。
我会做一个功能,像stopDoing(noThis),如果这个功能具有noThis参数集,它会调用每个停止功能,而不是在参数之一。如果没有设置参数,它会调用所有停止功能......类似的东西! –
如何停止所有功能,然后开始所需功能。 – gusaindpk
几乎肯定是一个XY问题。你介意分享你为什么需要这样的原因吗?这些事情是从什么开始和停止的,什么决定了他们是否需要开始或停止? – Amadan