1.多行语句letnum=0;letnewColor='blue'letnameList=[]/*------------这种格式不再优雅了------------*/letnum=0,newColor='blue',nameList=[];//改为---/也常用于两个变量值的交换let[num,newColor,nameList]=[0,'blue',[]]2。后台参数状态码转换开关(num){case0:status='待处理'break;案例1:status='processing'break;case2:status='processed''break;}//数字状态码转文本conststatus=['tobeprocessed','processing','processed'][num]//文本转数字状态码conststatus=['待处理','处理中','处理中'].indexOf('处理中')3、属性名转中文switch(str){case"name":return'name';案例“年龄”:返回“年龄”;case"city":return'city'default:return'noitem'}//改为constnewObj={name:"name",age:"age",city:"city",}returnnewObj[str]||'Noitem'4.将if赋值语句改为三元表达式if(code===200){tips='上传成功'}else{tips='上传失败'}//改为consttips=code===200?'上传成功':'上传失败'5.空值合并(0也算一个值)if(num||num==0){returnnum}else{return'-'}//改为---/如果num的值为undefined或null,则返回'-'占位符num??'-'6,multiple"or"判断if(city=='深圳'||city=='广州'||city=='上海'||city=='北京'){}//改为if(['深圳','广州','上海','北京'].includes(city)){}7.链式判断constnewObj={name:"胖尸",address:{street:"博兰街"}}if(newObj&&newObj.address&&newObj.address.street){}//改为if(newObj?.address?.street){alert('好家伙,胖子,你藏得够深的~')}8.对象合并---数组合并---数组去重constparams1={startTime:"",endTime:"",}constparams2={...params1,name:"",}constnewArr1=['用户1','用户2','用户3']constnewArr2=[...newArr1,'用户4','用户5']constnewArr3=[1,1,2,2,3,3,4,5,6,6,7,8,8,9,10,10]constnewArr4=[...新集合(newArr3)]9。对象解构constnewObj={name:"她的名字突然出现在我的脑海里",age:22}constname=newObj.name;constage=newObj.age;//改为const{name,age}=newObj10,Mathmethod/*Math.floorrounding*/letnum=10.56Math.floor(num)//10~~num//10/*Math.pow幂运算*/let[min,max]=[2,6]Math.pow(min,max)//64min**max//6411,字符串补全长度//预补全---/常用于完成时间位数'9'.padStart(2,'0')//09//Postfill'Excellent'.padEnd(2,'Etc')//Excellent12、ES2021最新逻辑赋值运算符??=、&&=和||=if(值){值=值}else{value='defaultvalue'}//等值??='defaultvalue'//例子10??='defaultvalue'//10null??='defaultvalue'//默认值13,判断长度赋值if(arr.length===0){status=false;}else{status=true;}//改成1个三元表达式status=arr.length?true:false;//改为2为布尔值status=!!arr.length;其实就是我在项目中经常用到的ES6/7/8/9/10/11/12的一些新特性,一定程度上简化了代码量。看起来也更优雅。补充建议:前端开发规范>>>
