1. Array method array.includes
Can replace a large number or statement.
Requirements: Test animals for
function printAnimals(animal) {
if (animal === 'dog' || animal === 'cat') {
console.log(`I Have a ${animal}`);
}
}
printAnimals('dog')
Copy the code
Optimized to get rid of the embarrassment of writing or
function printAnimals(animal) {
const animals = ['dog', 'cat', 'hamster']
if (animals.includes(animal)) {
console.log(animal);
}
}
printAnimals('dog')
Copy the code
2. Exit early/return early
Requirement: no animal throws an exception, if yes, print animal type, name, sex
function printAnimalsDetails(animal) { var result = null if (animal) { if (animal.type) { if (animal.name) { if (animal.genter) { result = `${animal.name} is a ${animal.genter} ${animal.type}` } else { result = 'no animal genter' } } else { result = 'no animal name' } } else { result = 'no animal type' } } else { result = 'no animal' } return result } console.log(printAnimalsDetails()); Console. log(printAnimalsDetails({type: 'dog ', name:' boonie ', genter: 'boonie ',}));Copy the code
Use early exit, early return optimization code
const printAnimalsDetails = ({ type, name, genter } = {}) => { if (! type) return 'no animal type' if (! name) return 'no animal name' if (! genter) return 'no animal genter' return `${name} is a ${genter} ${type}` } console.log(printAnimalsDetails()); Console. log(printAnimalsDetails({type: 'dog ', name:' boonie ', genter: 'boonie ',}));Copy the code
Copy the code
3. Object literals instead of switch statements
function printFruits(color) {
switch (color) {
case ' red':
return ['apple ']
case 'yellow':
return ['banana']
default:
return []
}
}
console.log(printFruits(null));
console.log(printFruits('yellow'));
Copy the code
Object literals are optimized
const fruitsColor = {
red: ['apple'],
yellow: [' banana']
}
function printFruits(color) {
return fruitsColor[color] || []
}
console.log (printFruits(null))
console.Log(printFruits('yellow'))
Copy the code
4. Default parameter structure map
The original code
var obj1 = { name: ' zs ' } var obj2 = { name: 'ls' } var obj3 = { [obj2]: '22', [obj1]: // var obj3 = {// '[object object]': '22', // '[object Object]': '11', // } console.log(obj3) //11,Copy the code
The map to rewrite
Ps: Map is described in detail in the previous article
const fruitsColor = new Map().set('red', [' apple']).set('yellow', ['banana'])
function printFruits(color) {
return fruitsColor.get(color) || []
console.Log(printFruits(null))
console.log(printFruits('yellow'))
}
Copy the code
5. Use Array. Some Array
Every method
Requirements: Test to see if all fruits are red
Const fruits = [{name: 'apple', color: 'red'}, {name: 'banana', color: 'red'} 'yellow' },] function test() { const isAlLRed = fruits.every(f => f.color == 'red') console.log(isAlLRed); } test()Copy the code
Some methods
Requirement: Test all fruits for red color
const fruits = [{
name: 'apple',
color: 'red'
}, {
name: 'banana',
color: 'yellow'
}, ]
function test() {
const isAlLRed = fruits.some(f => f.color == 'red')
console.log(isAlLRed);
}
test()
Copy the code