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. Java框架之Java Bean

    链接 知乎https://www.zhihu.com/question/19773379 总结 符合一定规范的编写的Java类,不是一种技术,而是一种规范.大家对于这种规范,总结了很多开发技巧,工具函 ...

  2. P2264 情书 Trie匹配

    \(\color{#0066ff}{题目描述}\) 为了帮助CYY,我们定义一个量化情书好坏的标准感动值.判断感动值的方法如下: 1.在情书的一句话中若含有给定词汇列表中的特定单词,则感动值加1,但每 ...

  3. (转)接口自动化测试之http请求实践总结

    一.接口测试的基本思路 1.确定要测试接口的请求类型.接口是get请求还是post请求. 2.确定接口的参数.需要传输的参数有哪些,类型分别是什么,都有哪些要求等. 3.按照参数要求构造请求需要的参数 ...

  4. 解读 pytorch对resnet的官方实现

    地址:https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py 贴代码 import torch.nn as ...

  5. 8.9zju集训日记

    和新队员的第一次比赛,前期开题方向基本正确,签到的速度比较快,中期读了旋转卡壳,矩阵和km的三道题目,都有一定的想法,但三个人意见不同没有往一个方向想,但其实旋转卡壳和km的题目思路几乎都对了,但是旋 ...

  6. linux dns高速缓存

    1.安装部署dns yum install bind -y systemctl start  named systemctl  enable  named systemctl  stop  firew ...

  7. 斑马打印机客户端GET和POST,以及后端两种打印方式。

    斑马打印机客户端GET和POST,以及后端两种打印方式. 背景环境:打印机安装在客户端外网.当用户登录时,通过ajax取服务器数据,返回打印机命令,然后客户端通过JS发送给斑马打印机. 1.使用Get ...

  8. spark Failed to get database default, returning NoSuchObjectException

    解决方法:1)Copy winutils.exe from here(https://github.com/steveloughran/winutils/tree/master/hadoop-2.6. ...

  9. tcp发送缓冲区学习

    https://blog.csdn.net/ysu108/article/details/7764461 Nginx 模块开发书上有内容   陶辉博客也可以

  10. Technical Committee Weekly Meeting 2016.06.21

    Meeting time: 2016.June.21 1:00~2:00 Chairperson:  Thierry Carrez Meeting summary: 1.Add current hou ...