Javascript 删除if语句
我在制造一个不和谐的机器人。要在用户键入命令时加载该命令,我需要验证该命令是否存在,但现在我有三个几乎相同的if语句。有没有一种方法可以把所有的东西都用一个表达式表达出来Javascript 删除if语句,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我在制造一个不和谐的机器人。要在用户键入命令时加载该命令,我需要验证该命令是否存在,但现在我有三个几乎相同的if语句。有没有一种方法可以把所有的东西都用一个表达式表达出来 if (!client.commands[cat]) return; if (client.commands[cat][command]) { if (client.commands[cat][command].conf) { if (client.elevation
if (!client.commands[cat]) return;
if (client.commands[cat][command]) {
if (client.commands[cat][command].conf) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else if (!args[0]) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else if (!client.commands[cat][args[0]]) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else { ...
if(!client.commands[cat])返回;
if(client.commands[cat][command]){
if(client.commands[cat][command].conf){
if(client.elevationManager.calculateGuildElevation(message.author.id,message.guild)
只需使用|
(或)操作符即可
if(!client.commands[cat])返回;
if(client.commands[cat][command]){
if(client.commands[cat][command].conf | | |!args[0]| | |!client.commands[cat][args[0]]{
if(client.elevationManager.calculateGuildElevation(message.author.id,message.guild)
@VLAZ对此有一个更好的答案,显示了更好的编程实践,去看看他的解决方案。只需使用
|
(or)操作符即可
if(!client.commands[cat])返回;
if(client.commands[cat][command]){
if(client.commands[cat][command].conf | | |!args[0]| | |!client.commands[cat][args[0]]{
if(client.elevationManager.calculateGuildElevation(message.author.id,message.guild)
@VLAZ对此有一个更好的答案,显示了更好的编程实践,去看看他的解决方案。让我更抽象地描述一下代码的样子:
if(主条件1){
如果(子条件1)返回结果1;
如果(分条款2)
如果(子条件3)返回结果2;
返回结果3;
}如果(2){
如果(子条件1)返回结果1;
如果(分条款2)
如果(子条件3)返回结果2;
返回结果3;
}否则,如果(主条件3){
如果(子条件1)返回结果1;
如果(分条款2)
如果(子条件3)返回结果2;
返回结果3;
}
这样可以更容易地对代码进行推理,也可以更容易地看到重复的确切位置以及重复的时间
if(子条件1)返回结果1;
如果(分条款2)
如果(子条件3)返回结果2;
返回结果3;
在不同条件下出现三次
首先
如果(A){
如果(B){
doSomething()
}
}
行为相同(可以简化为):
if(A&B){
doSomething()
}
由于内部代码仅在条件A
和条件B
均为真时执行。这是第一步,重复部分可缩短为:
if(子条件1)返回结果1;
if(子条件2和子条件3)返回结果2;
返回结果3;
接下来是构造:
if(A){
doSomething()
}若否(二){
doSomething()
}
也可以表示为简单布尔代数:
if(A | | B){
doSomething()
}
因为如果至少有一个条件是正确的,将执行内部代码
将此规则落实到位后,三个重复部分(在三种不同条件下)将塌陷为一个块:
if(主条件1 | |主条件2 | |主条件3){
如果(子条件1)返回结果1;
if(子条件2和子条件3)返回结果2;
返回结果3;
}