项目中不可忽视的JavaScript优化技巧

Null、Undefined、空检查普通写法:
if (username1 !== null || username1 !== undefined || username1 !== '') {let username = username1;}优化后:
let username = username1 || '';存在多个条件的判断普通写法:
if (x === 'png' || x === 'jpeg' || x === 'jpg' || x === 'gif') {//下一步}优化后:
if (['png', 'jpeg', 'jpg', 'gif'].includes(x)) {//下一步}三目运算符普通写法:
let flagif(x > 10){flag = true}else {flag = false}优化后:
let flag = x > 10 ? true : false空/未定义检查和分配默认值普通写法:
if(test1 !== null || test1 !== undefined || test1 !== ""){let test2 = test1;}else {let test2 = ''}优化后:
let test2 = test1 || ''箭头函数普通写法:
【项目中不可忽视的JavaScript优化技巧】function add(a, b) {return a + b; } 优化后写法:
const add = (a, b) => a + b;扩展运算符普通写法:
const data = https://www.isolves.com/it/cxkf/yy/js/2021-10-28/[1, 2, 3];const test = [4 ,5 , 6].concat(data);优化后写法:
const data = https://www.isolves.com/it/cxkf/yy/js/2021-10-28/[1, 2, 3];const test = [4 ,5 , 6, ...data];





    推荐阅读