gpt4 book ai didi

javascript - 有没有更好的方法来编写这个 mutuple 或 conditional ?

转载 作者:行者123 更新时间:2023-12-01 08:01:11 24 4
gpt4 key购买 nike

我在 javascript 中有以下 IF 语句:

if ( !(cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'JustifyFull') )

关于如何以更简洁的方式编写它有什么建议吗?

谢谢

最佳答案

if(!cmd.match(/^Justify(Left|Right|Center|Full)$/))

为了回应一些评论,您还可以通过小的编辑来模仿您的严格比较:

if( typeof cmd != 'String' || !cmd.match(/^Justify(Left|Right|Center|Full)$/))

这将以与您当前代码完全相同的方式使用react,忽略任何不是字符串的内容。

我个人认为您不太可能需要它。

关于javascript - 有没有更好的方法来编写这个 mutuple 或 conditional ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/968646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com