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. P2597 [ZJOI2012]灾难

    \(\color{#0066ff}{ 题目描述 }\) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝, ...

  2. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  3. linux heap堆分配

    heap堆分配在用户层面:malloc函数用于heap内存分配 void* malloc(size_t size); 进程的虚拟内存地址布局: 对用户来说,主要关注的空间是User Space.将Us ...

  4. 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)

    Level:   Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...

  5. x0vncserver

    x0vncserver -display :0 -passwordfile ~/.vnc/passwd

  6. 记录下自己安装cuda以及cudnn

    之前已经装过一次了,不过没有做记录,现在又要翻一堆博客安装,长点记性,自己记录下. 环境 ubuntu16.04 python2.7 商家送过来时候已经装好了显卡驱动,所以省去了一大麻烦. 剩下的就是 ...

  7. 【Cracking the Code Interview(5th edition)】一、数组与字符串(C++)

    1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟 ...

  8. C语言中的头文件

    1.头文件#include <> :表示引用标准库头文件,编译器会从系统配置的库环境中去寻找 2.头文件#include "":一般表示用户自己定义使用的头文件,编译器 ...

  9. Linux下Redis安装使用,主从模式,哨兵模式与PHP扩展(PHP7适用)

    一,软件准备 #redis wget http://download.redis.io/releases/redis-3.0.7.tar.gz #phpredis 非php7使用 下载后文件名为:ph ...

  10. python 文件与异常

    ####文件### r: -只能读 不能写 -读取文件不存在,是会报错 r+: - 可以执行读写操作; - 文件不存在, 报错: w: -只能写,不能读 -会清空文件内容 -文件不存在,会新建文件 w ...