JavaScript中条件语句的优化技巧总结

zasmylzn 12 0 PDF 2021-02-01 08:02:29

对多个条件使用 Array.includes function test(fruit) { if (fruit == 'apple' || fruit == 'strawberry') { console.log('red'); } } 上面的例子看起来不错。然而,如果还有更多红颜色的水果需要判断呢,比如樱桃和小红莓,我们要用更多的 ||来扩展这个表述吗? 我们可以用 Array.includes 重写上面的条件! function test(fruit) { const redFruits = ['apple', 'strawberry', 'cherry', 'cranber

用户评论
请输入评论内容
评分:
暂无评论