提前返回 return
function getData(type) {
if (!data) return '';
}
function getData(type) {
if (data) {
} else {
}
}
常量封装
export const SUBSCRIBE_STATUS_LIST = [
{
value: 'Y',
name: '已通过'
},
{
value: 'N',
name: '已拒绝'
},
{
value: '0',
name: '待审核'
}
];
export const TIME_UNIT_MAP = {
'd': 'DAY',
'm': 'MONTH',
'h': 'HOUR'
}
使用 includes()
const array =
['path1', 'path2', 'path3'];
if (array.includes(path)) {
}
if (path === 'path1' ||
path === 'path2' ||
path === 'path3') {
}
真值判断简写
if (tmp === true) or if (tmp !== "") or if (tmp !== null)
if (test1)
多条件的 && 运算符
if (test1) {
callMethod();
}
test1 && callMethod();
三目运算符实现短函数调用
function test1() {
console.log('test1');
};
function test2() {
console.log('test2');
};
var test3 = 1;
if (test3 == 1) {
test1();
} else {
test2();
}
(test3 === 1? test1:test2)();
对象属性解构简写
let test1 = 'a';
let test2 = 'b';
let obj = {test1: test1, test2: test2};
let obj = {test1, test2};
String 强制转换 Number
'12' * 1
+'12'
+'1a'
奇偶数判断
!!(3 & 1)
!!(4 & 1)