首页 > 编程语言 > 详细

javaScript-条件语句优化

时间:2019-03-01 17:57:32      阅读:127      评论:0      收藏:0      [点我收藏+]

1、多重判断时使用 Array.includes

test = (fruit: string) => {
    if (fruit == ‘apple‘ || fruit == ‘strawberry‘....) {
      console.log(‘apple or strawberry‘);
    }
  }
test = (fruit: string) => {
    const fruits = [‘strawberry‘,‘apple‘];
    if (fruits.includes(fruit)) {
      console.log(‘apple or strawberry‘);
    }
  }

 2、使用默认参数和解构

在JavaScript中我们总是需要检查 null / undefined的值和指定默认值:

test = (fruit: string, quantity?: any) => {
    if (!fruit) {
      return;
    }
    console.log(quantity || 0)
  }

我们可以通过声明 默认函数参数

test = (fruit: string, quantity: any = 20) => {
    if (!fruit) {
      return;
    }
    console.log(quantity)
  }

 

javaScript-条件语句优化

原文:https://www.cnblogs.com/Nyan-Workflow-FC/p/10457526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!