1. 空对象在数组中不认为是空
1 2 3 4 5 6
| if ({}) { console.log("this is not empty"); } else { console.log("this is empty"); }
|
2. prefer template 模式
链接地址 : https://eslint.org/docs/rules/prefer-template
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const urlPart = Object.keys(params) .map(function (k) { return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]); }) .join("&");
const urlPart = Object.keys(params) .map((k) => { const encodeA = encodeURIComponent(k); const encodeB = encodeURIComponent(params[k]); return `${encodeA}=${encodeB}`; }) .join("&");
|
3. 不写行内函数 (no-inner-declarations)
链接地址: https://eslint.org/docs/rules/no-inner-declarations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| const handleRender = (h, params) => { if (params.row.can_handle === true) { function btn(prop) { return h('btn', { props : prop, on : { click : () => { that.onDoClick(params); } } }); } let prop = {}; prop = { type : 'primary', icon : 'ios-compose', title : '待提现', }; return btn(prop); } } return ''; }
const handleRender = (h, params) => { if (params.row.can_handle === true) { let prop = {}; prop = { type : 'primary', icon : 'ios-compose', title : '待提现', }; return h('btn', { props : prop, on : { click : () => { that.onDoClick(params); } } }); } } return ''; }
|
4. 多余的 “,”
1 2 3 4 5
| const { status, message, data } = response.data;
const { status, message, data } = response.data;
|
语雀镜像 : Javascript(Code Reivew) - 01 ,点此 提问