1.对多个条件使用Array.includes

eg: function test(fruit){                                                                                                                function test(fruit){

if(fruit=='apple' || fruit=='cherry' ){                        可改写为

console.log('red')                        =================================>>          const redFruits=['apple','cherry','strawberry'];

}                                                                                                                                       if(redFruits.includes(fruit)){

console.log('red')

}

}                                                                                                                                               }

2.更少的嵌套,尽早的返回

eg: 如果没有水果名称,抛出错误

如果红色水果数量超过10个,接受并打印

function test(fruit, quantity){

const redFruits=['apple','cherry','strawberry'];

if(!fruit) throw new Error('No fruit!');

if(!redFruits.includes(fruit)) return;

console.log('red'');

if(quantity >10){

console.log('big quantity')

}

}

3.使用默认的函数参数和结构

4.选择Map或对象字面量,而不是switch语句

function test(color){

switch(color){

case 'red':

return ['apple','strawberry'];

case 'yellow':

return['banana','pineapple'];

case 'purple':

return ['grape','plum'];

default:

reutrn [];

}

}

test(null)

test('yellow')

||||||||||

||||||||||  代码改造后

||||||||||

||||||||||

||||||||||

改法一:const fruitColor={

red:['apple','strawberry'],

yellow:['banana','pineapple'],

purple:['grape','plum']

}

function test(color){

return fruitColor[color] || [];

}

改法二:

const fruitColor=new Map()

.set('red',['apple','strawberry'])

.set('yellow',['banana','pineapple'])

.set('purple',['grape','plum'])

function test(color){

return fruitColor.get(color) ||[]

}

改法三:

const fruits=[

{name:'apple',color:'red'},

{name:'strawberry',color:'red'},

{name:'banana',color:'yellow'},

{name:'pineapple',color:'yellow},

]

function test(color){

return fruits.filter(f>=f.color==color)

}

5.所有或部分使用Array.every&Array.some的条件

eg:检查所有的水果是否都是红色的

const fruits=[

{name:'strawberry',color:'red'},

{name:'banana',color:'yellow'},

]

function test(){

const isAllRed=fruits.every(f>=f.color=='red');

console.log(isAllRed)

}

eg:判断任何一种水果是否为红色

const fruits=[

{name:'strawberry',color:'red'},

{name:'banana',color:'yellow'},

]

function test(){

const isAllRed=fruits.some(f>=f.color=='red');

console.log(isAllRed)

}

JS条件语句优化的更多相关文章

  1. 优化 JS 条件语句的 5 个技巧

    优化 JS 条件语句的 5 个技巧 原创: 前端大全 前端大全 昨天 (给前端大全加星标,提升前端技能) 编译:伯乐在线/Mr.Dcheng http://blog.jobbole.com/11467 ...

  2. 优化 JS 条件语句及JS 数组常用方法, ---- 看完绝对对日后开发有用

    前言: 日常所说的优化优化.最后我们到底优化了哪些,不如让我们从代码质量开始:个人觉得简洁简化代码其实觉得存在感挺强烈的QAQ 1. 获取URL中 ?后的携带参数: 这是我见过最简洁的了,若有更简洁的 ...

  3. js - 总结一下条件语句优化

    [笔记] // 简单的语句用三目运算符也可以的(除了需要return的) 1 == 1 ? console.log('执行了...1') : console.log(); 1 == 2 ? conso ...

  4. js条件语句,用if...else if....else方程ax2+bx+c=0一元二次方程。求根

    if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码 if...else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码 if...else if... ...

  5. JAVAscript学习笔记 js条件语句 第三节 (原创) 参考js使用表 (2017-09-14 15:55)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. js条件语句初步练习

    var a=18            if(a<10){                alert("便宜")            }            else{  ...

  7. js条件语句之职责链数组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS 优化条件语句的5个技巧

    前言 在使用 JavaScript 的时候,有时我们会处理大量条件语句,这里有5个技巧帮助我们编写更简洁的条件语句. 一.对多个条件使用 Array.includes 例子: function con ...

  9. js中的条件语句

    //js中的条件语句 ; //example1 单分支语句 ){ console.log("你已经不年轻了!"); }else{ console.log("你依然很有活力 ...

随机推荐

  1. Python 文件和异常

    一.从文件中读取数据 #!/usr/bin/env python with open('pi') as file_object: contents = file_object.read() print ...

  2. php excel开发01

    启用cache

  3. Chrome 67 以后版本无法离线安装crx插件

    原文链接:https://blog.csdn.net/wanwuguicang/article/details/80716178 升级了Chrome后无法离线安装扩展 如图: 谷歌自Chrome 67 ...

  4. Spark大数据处理 之 从WordCount看Spark大数据处理的核心机制(2)

    在上一篇文章中,我们讲了Spark大数据处理的可扩展性和负载均衡,今天要讲的是更为重点的容错处理,这涉及到Spark的应用场景和RDD的设计来源. Spark的应用场景 Spark主要针对两种场景: ...

  5. C语言数据结构-顺序线性表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-顺序线性表的实现-C语言 #define MAXSIZE 100 //结构体定义 typedef struct { int *elem; //基地址 int length; //结构体当 ...

  6. Number BZOJ3275 最大流

    有N个正整数,需要从中选出一些数,使这些数的和最大. 若两个数a,b同时满足以下条件,则a,b不能同时被选 1:存在正整数C,使a*a+b*b=c*c 2:gcd(a,b)=1 Sample Outp ...

  7. 7.Palindrome Linked List(回文链表)

    Level: ​  Easy 题目描述: Given a singly linked list, determine if it is a palindrome. Example 1: Input: ...

  8. centos yum命令找不到包

    https://blog.csdn.net/joe_le/article/details/80107832 yum  install epel-release  //扩展包更新包 yum  updat ...

  9. Jenkins的Pipeline脚本在美团餐饮SaaS中的实践(转)

    一.背景 在日常开发中,我们经常会有发布需求,而且还会遇到各种环境,比如:线上环境(Online),模拟环境(Staging),开发环境(Dev)等.最简单的就是手动构建.上传服务器,但这种方式太过于 ...

  10. eclipse项目中的java文件导入后变为空心J问题

    1,选择工程名字右键——>属性——>JavaBuild Path会看到右侧Source标签中为空白 2,点击Add Folder...选择如图所示的src包和相关的配置文件包,点击确定: ...