Javascript 优雅地切换许多功能
假设我有一些函数可以做一些事情:Javascript 优雅地切换许多功能,javascript,jquery,function,object,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() { consol
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'
在迭代所有不匹配的stop
函数时,是否有办法启用一个do
函数
例如,如果我做var what='this';做[什么]()代码>,我希望它不仅doThis()代码>但也stoptuff();停止事物();停止();停止那个()代码>
我很难想出一个优雅的方法,它不涉及冗长的if语句或case/switch。您可以在下面的代码中使用一个简单的for循环
如果您的所有do/stop功能都是全局的,则此功能有效
如果您的所有do/stop功能都是全局的,则此功能有效
函数doStuff(){console.log('doing stuff');}
函数doThings(){console.log('doing things');}
函数doIt(){console.log('doing');}
函数doThis(){console.log('doing this');}
函数doThat(){console.log('doing that');}
函数stopStuff(){console.log('stopping stuff');};
函数stopThings(){console.log('stopping things');};
函数stopIt(){console.log('stopping');};
函数stopThis(){console.log('stopping this');};
函数stopThat(){console.log('stopping that');};
var run=函数(根){
var fnNames=['stuff','things','it','this','that'];
返回函数(什么){
fnNames.forEach(函数(fn){
var fnName=fn[0].toUpperCase()+fn.slice(1);
根[(what==fn?'do':'stop')+fnName]();
});
};
}(窗口);
//用法
console.log('things');
跑(“东西”);
console.log(“this”);
运行(“本”)代码>让函数获取一个参数,并遍历一个函数数组,该数组调用除参数之外的每个函数。例如:
var doing = [
stuff: function(func) {
stopping.forEach(element => {
if (element !== func) {
element()
}
}
doStuff()
},
...
];
var stopping = [
stopStuff: function() { stoppingStuff() },
...
];
有两种方法可以做到这一点,这取决于运行额外功能的成本
在启动新的do函数之前调用所有的stop函数
或过滤特定的停止功能
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]();
我会做一个函数,比如stopdoon(noThis),如果这个函数没有设置这个参数,它会调用每个stop函数,而不是参数上的那个。如果没有设置参数,它将调用所有停止函数。。。差不多吧!停止所有函数,然后启动所需的函数如何?几乎肯定是XY问题。你介意分享一下你需要这样东西的原因吗?这些东西的启动和停止是什么,是什么决定了它们是否需要启动或停止?@Amadan它们控制着各种动画效果。如果没有更多的细节,我可能会有一个形式为{name:“Stuff”,on:function(){…},off:function(){…}
,,然后在它们上面循环并运行effect.on()
或effect.off()
,这取决于它是否符合您的标准。但同样,没有足够的细节;我不喜欢那种认为它们只是随机扔进全局上下文中的函数的想法。在很多方面比我的好——尽管stuff:function(){doStuff();}
可以是stuff:doStuff
,以赢得:b与你的答案和Jaromanda的匹配。我真希望我能给你们两个荣誉。我不知道谁的效率更高。不管怎样,谢谢你!非常感谢。这对我来说是最有意义的,并且对我的用例非常有效!ES6的加分(在你移除它之前)。我可以把ES6back@daveycroqet-我已经将ES6放回第二个版本(我认为它实际上更通用)-添加了。排序停止所有操作,然后启动selected@JaromandaX不,我已经更新了小提琴作品。
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]();
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]();