// bad
if (a === 'a') {
b = true
} else {
b = false
}
// good
b = a === 'a'
// bad
if (arr.length !== 0) {
// todo
}
// good
if (arr.length) {
// todo
}
// bad
if (arr.length === 0) {
// todo
}
// good
if (!arr.length) {
// todo
}
// bad
if (a === 'a') {
b = a
} else {
b = c
}
// good
b = a === 'a' ? a : c
// bad
if (a === 1 || a === 2 || a === 3 || a === 4) {
// todo
}
// good
let arr = [1, 2, 3, 4]
if (arr.includes(a)) {
// todo
}
巧用数组方法,尽量避免用for循环
// bad
let arr = [1, 3, 5, 7]
function isHasNum (n) {
for (let i = 0; i < arr.length; i ++) {
if (arr[i] === n) {
return true
}
}
return false
}
// good
let arr = [1, 3, 5, 7]
let isHasNum = n => arr.some(num => num === n)
// best
let arr = [1, 3, 5, 7]
let isHasNum = (n, arr) => arr.some(num => num === n)
// bad
for (let i = 0; i < arr.length; i ++) {
// todo
arr[i].key = balabala
}
// good
arr.forEach(item => {
// todo
item.key = balabala
})
// bad
let arr = [1, 3, 5, 7],
newArr = []
for (let i = 0; i < arr.length; i ++) {
if (arr[i] > 4) {
newArr.push(arr[i])
}
}
// good
let arr = [1, 3, 5, 7]
let newArr = arr.filter(n => n > 4) // [5, 7]
// bad
let arr = [1, 3, 5, 7],
newArr = []
for (let i = 0; i < arr.length; i ++) {
newArr.push(arr[i] + 1)
}
// good
let arr = [1, 3, 5, 7]
let newArr = arr.map(n => n + 1) // [2, 4, 6, 8]
巧用对象方法,避免使用for...in
let obj = {
a: 1,
b: 2
}
// bad
let values = []
for (key in obj) {
values.push(obj[key])
}
// good
let values = Object.values(obj) // [1, 2]
let obj = {
a: 1,
b: 2
}
// bad
let keys = []
for (value in obj) {
keys.push(value)
}
// good
let keys = Object.keys(obj) // ['a', 'b']
巧用解构简化代码
// bad
let a = 1,
b = 2
let temp = a
a = b
b = temp
// good
let a = 1,
b = 2
[b, a] = [a, b]
// bad
setForm (person) {
this.name = person.name
this.age = person.age
}
// good
setForm ({name, age}) {
this.name = name
this.age = age
}
有的后端返回的键名特别长,你可以这样干
// bad
setForm (data) {
this.one = data.aaa_bbb_ccc_ddd
this.two = data.eee_fff_ggg
}
// good
setForm ({aaa_bbb_ccc_ddd, eee_fff_ggg}) {
this.one = aaa_bbb_ccc_ddd
this.two = eee_fff_ggg
}
// best
setForm ({aaa_bbb_ccc_ddd: one, eee_fff_ggg: two}) {
this.one = one
this.two = two
}
// bad
setForm ({name, age}) {
if (!age) age = 16
this.name = name
this.age = age
}
// good
setForm ({name, age = 16}) {
this.name = name
this.age = age
}
let person = {
name: '张三',
age: 38
}
let name = person.name || '佚名'
let person = {
name: '张三',
age: 38,
children: {
name: '张小三'
}
}
let childrenName = person.children && person.childre.name
let person = {
name: 'LiMing',
age: 18
}
// bad
function sayHi (obj) {
console.log('大家好,我叫' + person.name = ',我今年' + person.age + '了')
}
// good
function sayHi (person) {
console.log(`大家好,我叫${person.name},我今年${person.age}了`)
}
// best
function sayHi ({name, age}) {
console.log(`大家好,我叫${name},我今年${age}了`)
}
let arr [18, 19, 20, 21, 22]
// bad
function findStudentByAge (arr, age) {
return arr.filter(function (num) {
return num === age
})
}
// good
let findStudentByAge = (arr, age)=> arr.filter(num => num === age)
// bad
let findStudentByAge = (arr, age) => {
if (!age) throw new Error('参数不能为空')
return arr.filter(num => num === age)
}
// good
let checkoutType = () => {
throw new Error('参数不能为空')
}
let findStudentByAge = (arr, age = checkoutType()) =>
arr.filter(num => num === age)