Objects
1.Use the literal syntax for object creation
// good const item = {};
Arrays
1.Use the literal syntax for array creation
// good const items = [];
2.Use Array#push instead of direct assignment to add items to an array.
const someStack = []; // good someStack.push('abracadabra');
3.Use array spreads … to copy arrays.
// good const itemsCopy = [...items];
Iterators and Generators
Don’t use iterators. Prefer JavaScript’s higher-order functions instead of loops like for-in or for-of
const numbers = [1, 2, 3, 4, 5]; // good let sum = 0; numbers.forEach((num) => { sum += num; }); sum === 15; // best (use the functional force) const sum = numbers.reduce((total, num) => total + num, 0); sum === 15;
Variables
Avoid using unary increments and decrements (++, –).
// good const array = [1, 2, 3]; let num = 1; num += 1; num -= 1; const sum = array.reduce((a, b) => a + b, 0); const truthyCount = array.filter(Boolean).length;
近期评论