1.条件语句使用Object
const actions = {
1 : 'processing' ,
2 : 'fail' ,
3 : 'success' ,
4 : 'cancel' ,
5 :funtion ( ) {
console. log ( '你好啊,我是5' )
}
default : 'other' ,
} ;
console. log ( actions[ status] ?? actions. default) ;
2.使用声明式
const result= dataSource. filter ( ( item ) => { item. age> 10 } )
let result= [ ]
dataSource. forEach ( ( item ) => {
if ( item. age> 10 ) {
result. push ( item)
}
} )
3.给定默认值
1.函数
function handler ( config ) {
config. map ( ( item ) => {
item. content= Number ( item. comtent)
} )
}
function handler ( config= [ ] ) {
config. map ( ( item ) => {
item. content= Number ( item. comtent)
} )
}
2.解构赋值
function handleArr ( arr ) {
let [ a, b, c] = [ 1 , 2 , 3 ]
return a + b + c
}
function handleArr ( arr ) {
let [ a= 1 , b= 2 , c= 3 ] = arr
return a+ b+ c
}
const arr= [ 1 , 2 ]
handlerArr ( arr)
function handleObj ( obj ) {
const { name, list = [ ] } = obj
if ( name) {
list. map ( ( item ) => {
console. log ( item)
} )
}
}
const obj = {
name : 'add1' ,
list : [ 1 , 2 , 3 ]
}
async function handleData ( ) {
const { code, msg, data = [ ] } = await fetchList ( )
Array. isArray ( data) && data. map ( ( item ) => {
console. log ( item)
} )
}
4.includes优化代码
function verifyIdentity ( identityId ) {
if ( identityId== 1 || identityId== 2 || identityId== 3 || identityId== 4 ) {
return '你的身份合法,请通行!'
} else {
return '你的身份未知,警告!'
}
}
function verifyIdentity ( identityId ) {
if ( [ 1 , 2 , 3 , 4 ] . includes ( identityId) ) {
return '你的身份合法,请通行!'
} else {
return '你的身份未知,警告!'
}
}
function verifyIdentity ( identityId ) {
return [ 1 , 2 , 3 , 4 ] . includes ( identityId) ? '你的身份合法,请通行!' : '你的身份未知,警告!'
}
5.if else语句
1.单个if else优化
let flag = true
if ( flag) {
} else {
return ;
}
let flag = true
if ( ! flag) return ;
或者:
flag && callMethod ( )
2.单个if else带返回值优化
function demo ( flag ) {
if ( flag) {
return "真"
} else {
return "假"
}
}
function demo ( flag ) {
return flag? "真" : "假"
}
3.单个 if else 执行不同方法 优化 同上
function success ( ) {
console. log ( "success" )
}
function fail ( ) {
console. log ( "fail" )
}
function demo ( flag ) {
if ( flag) {
success ( )
} else {
fail ( )
}
}
function demo ( flag ) {
flag? success ( ) : fail ( )
}
function demo ( flag ) {
[ false , true ] . includes ( flag) && [ fail, success] [ Number ( flag) ] ( )
}
function demo ( flag ) {
[ fail, success] [ Number ( flag) ] ( )
}
4.多个if else嵌套优化
let result = {
status : 200 ,
codeMsg : 'success' ,
data : {
userInfo : {
age : 18 ,
hobby : [ '敲代码' , '打篮球' ]
}
}
}
if ( result. data && result. data. userInfo &&
result. data. userInfo. hobby &&
Array. isArray ( result. data. userInfo. hobby) &&
result. data. userInfo. hobby. length )
{
}
result. data &&
result. data. userInfo &&
result. data. userInfo. hobby &&
Array. isArray ( result. data. userInfo. hobby) &&
result. data. userInfo. hobby. length) &&
( ( ) => {
} ) ( )
try {
if ( result. data. userInfo. hobby. length) {
}
} catch ( error) {
}
if ( result?. data?. userInfo?. hobby?. length) {
}
5.多个if else带返回值 优化
function getPosition ( direction ) {
if ( direction == "left" ) {
return "左"
} else if ( direction == "right" ) {
return "右"
} else if ( direction == "top" ) {
return "上"
} else if ( direction == "bottom" ) {
return "下"
} else {
return "未知"
}
}
function getPosition ( direction ) {
return ( {
left : "左" ,
right : "右" ,
top : "上" ,
bottom : "下"
} ) [ direction] || "未知"
}
const directions= new Map ( [
[ 'left' , '左' ] ,
[ 'right' , '右' ] ,
[ 'top' , '上' ] ,
[ 'bottom' , '下' ] ,
] )
const getPosition ( direction ) => {
return directions. get ( direction) || '未知'
}
分支优化
1.简单分支优化
function getUserDescribe ( name ) {
const describeForNameMap = {
小刘 : ( ) => console. log ( "刘哥哥" ) ,
小红 : ( ) => console. log ( "小红妹妹" ) ,
陈龙 : ( ) => console. log ( "大师" ) ,
李龙 : ( ) => console. log ( "师傅" ) ,
大鹏 : ( ) => console. log ( "恶人" ) ,
} ;
describeForNameMap[ name] ? describeForNameMap[ name] ( ) : console. log ( "此人比较神秘!" ) ;
}
2.复杂分支优化
function getUserDescribe ( name ) {
const describeForNameMap = [
[
( name ) => name. length > 3 ,
( ) => console. log ( "名字太长" )
] ,
[
( name ) => name. length < 2 ,
( ) => console. log ( "名字太短" )
] ,
[
( name ) => name[ 0 ] === "陈" ,
( ) => console. log ( "小陈" )
] ,
[
( name ) => name === "大鹏" ,
( ) => console. log ( "管理员" )
] ,
[
( name ) => name[ 0 ] === "李" && name !== "李鹏" ,
( ) => console. log ( "小李" ) ,
] ,
] ;
const getDescribe = describeForNameMap. find ( ( item ) => item[ 0 ] ( name) ) ;
getDescribe ? getDescribe[ 1 ] ( ) : console. log ( "此人比较神秘!" ) ;
}
getUserDescribe ( '齐天大圣' ) ( )
3.抽离分支
const describeForNameMap = {
小刘 : ( ) => console. log ( "刘哥哥" ) ,
小红 : ( ) => console. log ( "小红妹妹" ) ,
陈龙 : ( ) => console. log ( "大师" ) ,
李龙 : ( ) => console. log ( "师傅" ) ,
大鹏 : ( ) => console. log ( "恶人" ) ,
} ;
function getUserDescribe ( name ) {
describeForNameMap[ name] ? describeForNameMap[ name] ( ) : console. log ( "此人比较神秘!" ) ;
}
const describeForNameMap = [
[
( name ) => name. length > 3 ,
( ) => console. log ( "名字太长" )
] ,
[
( name ) => name. length < 2 ,
( ) => console. log ( "名字太短" )
] ,
[
( name ) => name[ 0 ] === "陈" ,
( ) => console. log ( "小陈" )
] ,
[
( name ) => name === "大鹏" ,
( ) => console. log ( "管理员" )
] ,
[
( name ) => name[ 0 ] === "李" && name !== "李鹏" ,
( ) => console. log ( "小李" ) ,
] ,
] ;
function getUserDescribe ( name ) {
const getDescribe = describeForNameMap. find ( ( item ) => item[ 0 ] ( name) ) ;
getDescribe ? getDescribe[ 1 ] ( ) : console. log ( "此人比较神秘!" ) ;
}
switch语句
switch ( type) {
case 'A' :
console. log ( 1 )
break
case 'B' :
console. log ( 2 )
break
case 'C' :
conlose. log ( 3 )
break ;
default :
console. log ( 0 )
}
console.log优化
console. log ( 'name' , name)
console. log ( { name} )
隐式转换
const price = parseInt ( '32' ) ;
const price = Number ( '32' ) ;
const price = + '32' ;
const timeStamp = new Date ( ) . getTime ( ) ;
const timeStamp = + new Date ( ) ;
console. log ( + true ) ;
console. log ( + false ) ;
const isTrue = Boolean ( '' )
const isTrue = ! ! ''
const isTrue = Boolean ( 0 )
const isTrue = ! ! 0
const isTrue = Boolean ( null )
const isTrue = ! ! null
const isTrue = Boolean ( undefined )
const isTrue = ! ! undefined
const num = 1 ;
const str = num. toString ( ) ;
const str = num + '' ;
vue中优化
1.v-if 多条件判断
普通写法 v- if = "condition === 0 || condition === 1 || condition === 2"
简洁写法 v- if = "[0, 1, 2].includes(condition)" < ! -- 括号里可以是字符串和数字-- >
2.不要在template中写很长的判断、运算,可以使用计算属性computed
< div>
{ {
fullName. split ( ' ' ) . map ( function ( word ) {
return word[ 0 ] . toUpperCase ( ) + word. slice ( 1 )
} ) . join ( ' ' )
} }
< / div>
< ! -- 在模板中 -- >
< div> { { normalizedFullName } } < / div>
computed : {
normalizedFullName : function ( ) {
return this . fullName. split ( ' ' ) . map ( function ( word ) {
return word[ 0 ] . toUpperCase ( ) + word. slice ( 1 )
} ) . join ( ' ' )
}
}