1、if else
if (bool) {
a =1;
} else {
a = 2
}
// 简写
a = bool ? 1 : 2;
2、if
if (bool) {
a = fn()
}
//简写
bool && (a=fn())
if (!bool) {
a = fn()
}
//简写
bool || (a=fn())
3、对象
if (b===1) {
a = 'one'
} else if (b===2) {
a='two'
} else {
return ''
}
//简写
let res = {1:'one',2:'two'}
a = res[key] ? res[key] : '';
4、if else if
if (b===1) {
a='one'
} else if (b===2) {
a = 'two'
} else {
return ''
}
//简写
let res ={1:'one', 2:'two'}
a = res[key] ? res[key] : '';
5、强制转换
!!'foo' // 强制转换为Boolean类型
+'45' // 转化为number类型
45+'' // 转化为string类型
(~~(Math.random()*(1<<24))).toString(16) //随机颜色
来源:https://www.cnblogs.com/vhen/p/7876090.html