【问题标题】:Switch case version of if(input.startsWith("string")) { ... } in JavaScript在 JavaScript 中切换 if(input.startsWith("string")) { ... } 的大小写版本
【发布时间】:2021-08-26 13:59:05
【问题描述】:

上下文:我正在制作一个具有命令处理程序的游戏。在该命令处理程序中,我正在尝试优化一段代码。

我正在尝试做一个 switch-case 语句,它允许我做与下面显​​示的完全相同的事情

input.startsWith('echo') ? ( History.add(input), commands.echo(input) ) 
: ( input.startsWith('history') ? (History.add(input), commands.history(input))
: input.startsWith("new") ? (History.add(input), commands.new(input) ) 
: input.startsWith('theme') ? (History.add(input), commands.theme(input)) 
: (developerMode == true && input == "test") ? (History.add(input), commands.test()) 
: input.startsWith("cd") ? (History.add(input), commands.cd(input))
: input.startsWith("find") ? (History.add(input), commands.find(input, type, title)) 
: (History.add(input), utils.message({ user: '$', command: input }, ` - bash: ${input}: command not found `, 'error')))

简化版:

if (input.startsWith('echo')) {
    History.add(input)
    commands.echo(input);
} else if (input.startsWith("history"))
    History.add(input)
    commands.history(input);
} else if (input.startsWith("new")) {
    History.add(input)
    commands.new(input);
} else if (input.startsWith('theme')) {
    History.add(input)
    commands.theme(input);
} else if (developerMode == true && input == "test") {
    History.add(input)
    commands.test()
} else if (input.startsWith("cd")) {
    History.add(input)
    commands.cd(input)
} else if (input.startsWith("find")) {
    History.add(input)
    commands.find(input, type, title)
} else {
    History.add(input)
    utils.message({ user: '$', command: input }, ` - bash: ${input}: command not found `, 'error');
} 

【问题讨论】:

  • 你不想要 switch/case 语句 - if 语句本身并没有错(三元表达式版本令人厌恶!)

标签: javascript switch-statement


【解决方案1】:

您的代码中有一些冗余。首先,您可以通过对commands 的键进行测试来检查您的命令是否有效(您应该尝试获取第一个不带字符串#startWith 的命令参数)。

正如评论中所述,您无需将 else-if 更改为 switch。没有收获。但是,在这两种情况下,您都必须添加代码来处理您的命令,既要填充您的命令对象,又要解析用户输入。

为了获得一定的灵活性和可重用性,您能做的最好的事情就是以相同的方式解析和执行所有命令。

假设您的命令由空格分隔,有一种方法可以:


const [ cmd, ...args ] = input.split(' ');//at index 0, you get your command, in args, you get your command args. It's called array destructuring
//Note that you may need a more complexe way to parse your command, but you should get the idea.

//Since you do it every time, you don't have to repeat it
History.push(input);

if(cmd in commands) commands[cmd](input, ...args);
else utils.message(
    { user: '$', command: input }, 
    ` - bash: ${input}: command not found `, 'error'
);

【讨论】:

  • 阿尔。您确实帮助我将一个 60 衬里的开关盒(其余的我没有发布)变成了一个 5 衬里。谢谢!我已将您的 If 语句变成了三元组以使其更整洁(我知道。它根本对性能没有影响。但我确实这样做了,为什么不这样做)。谢谢!
  • Np ;) 对于三元语法,随心所欲。就个人而言,我仅将它们用于条件赋值(如 const defaultBehaviour = something === 'none' ? conf.something : 'default' )并且从不做 else-if 或 switch satetments,因为它们难以阅读、推理和容易出错。但这取决于您的项目范围。如果你一个人并且觉得它可读,那没问题。
猜你喜欢
  • 2022-01-02
  • 2021-07-11
  • 2021-10-18
  • 2022-11-25
  • 1970-01-01
  • 2021-10-10
  • 2023-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多