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. 2019.2.14 考试T3 交互题

    \(\color{#0066ff}{ 题目描述 }\) 由于机房被成功拯救了,花_Q很高兴,花_Q生成了一个 0 到 N - 1 的排列(排列的下标从 0 到 N - 1 ).保证排列中 0 在 N ...

  2. eclipse 自定义 文档

    在这里写....

  3. nginx 安装第三方 模块

    查看nginx在安装时开启了哪些模块 如果你nginx是rpm包安装的,直接用如下命令nginx -V 如果你是源码包编译安装,假如你的安装路径是/usr/local/nginx,那么你可以使用: / ...

  4. jq ajax超时设置

    var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...

  5. day14 面向对象

    . 面向对象和面向过程 .面向过程:核心是过程,是流水线 优缺点: .流程化,编写简单 .可扩展性差 .面向对象:核心是对象(对象:具有相同属性和动作的结合体) 优缺点: .可扩展行强 .复杂度高于面 ...

  6. Linux磁盘分区管理

    1.分区步骤          fdisk -l                                  查看系统中的磁盘 fdisk /dev/vdb                   ...

  7. delphi 与 java 兼容的 MD5

    function GetMd5(AValue : string) : string; var md5 : TIdHashMessageDigest5; bytes,byte1 : TBytes; be ...

  8. 阿里插件检查 lombok报错---方法缺少 '@Override' 注解

    问题: Eclipse里,阿里编码规约插件扫描代码出现,但是idea却没有. 解决: 将以上注解改成 @Setter @Getter @NoArgsConstructor @AllArgsConstr ...

  9. 爬虫(GET)——爬取多页的html

    工具:python3 目标:将编写的代码封装,不同函数完成不同功能,爬取任意页数的html 新学语法:with open as 除了有更优雅的语法,with还可以很好的处理上下文环境产生的异常. # ...

  10. mysql 存储过程(代码块)

    大纲: 创建.删除.调用. 声明变量.变量赋值 声明游标 声明异常处理器 判断 循环 使用心得 一.创建.删除.调用 创建 DELIMITER $$ #修改分隔符 )) #括号里是入参.IN代表传入的 ...