// bad
var a = 1;
// good
const a = 1;
// bad
var a = 1;
for (var i; i < 10; i++){
a = 1 + i;
}
// good
let a = 1;
for (let i; i < 10; i++){
a += i
}
// bad
const obj = new Object();
// good
const obj = {
};
// bad
const arr = new Array();
// good
const arr = [];
// bad
const str = 'world';
const newStr = 'hello' + str;
// good
const str = 'world';
const newStr = `hello ${
str}`;
// bad
const obj = {
val: 1,
fn: function() {
return obj.val + 1;
}
}
// good
const obj = {
val: 1,
fn(){
return obj.val++;
}
}
// bad
const value = 1;
const obj = {
value: value };
//good
const value = 1;
const obj = {
value };
// bad
const obj1 = {
a: 1, b: 2 };
let obj2 = {
c: 3, d: 4 };
obj2.a = obj1.a;
obj2.b = obj1.b;
// good
const obj1 = {
a: 1, b: 2 };
const obj2 = {
...obj1, c: 3, d: 4 };
// bad
const arr = [];
arr[arr.length] = "hello world";
// good
const arr = [];
arr.push("hello world");
// bad
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [6, 7, 8, 9, 10];
const arr = arr1.concat(arr2);
// good
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [6, 7, 8, 9, 10];
const arr = [...arr1, ...arr2];
// bad
function fn(obj) {
return `${
obj.name} ${
obj.value}`
}
// good
function fn({
name, value }) {
return `${
name} ${
value}`
}
// bad
const obj = {
a: 1, b: 2 };
const a = obj.a;
const b = obj.b;
// good
const obj = {
a: 1, b: 2 };
const {
a, b } = obj;
// bad
function fn() {
};
// bad
const fn = function() {
};
// good
const fn = () => {
};
// bad
const fn = (a, b) => {
if(!a) a = 1;
if(!b) b = 1;
return {
a, b };
}
// good
const fn = (a = 1, b = 2) => {
return {
a, b };
}
// bad
const arr= [1, 2, 3, 4, 5];
let total = 0;
arr.forEach( (n) => {
total += n})
// bad
const arr = [1, 2, 3, 4, 5];
let total = 0;
for (let i; i < arr.length; i++){
total += arr[i];
}
// good
const arr = [1, 2, 3, 4, 5];
const total = arr.reduce((total, num) => total + num);
// bad
const arr = [{
a: 1 }, {
a: 2 }, {
a: 3 }];
let exist = false;
arr.forEach(item => {
if(item.a === 2) exist = true;
});
// good
const arr = [{
a: 1 }, {
a: 2 }, {
a: 3 }];
const exist = arr.some(item => item.a === 2)
// bad
const a = 1;
const b = 1;
let isTrue = false;
if(a === b) {
isTrue = true;
}
// good
const a = 1;
const b = 1;
const isTrue = a === b;
// bad
const a = 5;
let b;
if(a === 5) {
b = 3;
} else {
b = 2;
}
// good
const a = 5;
const b = a === 5 ? 3 : 2;
// bad
const arr= [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// good
const arr= [1, 2, 3, 4, 5];
for(let val of heroes) {
console.log(val);
}
// bad
if(x === 'abc' || x === 'def' || x === 'ghi' || x === 'jkl'){
}
// good
if(['abc', 'def', 'ghi', 'jkl'].includes(x)){
}
// bad
let test;
if(x > 10){
test = true;
}else{
test = false;
}
// good
const test = x > 10;
// bad
if(data !== null || data !== undefined || data !== ''){
let arr = data;
}
// good
let arr = data || '';
// bad
const data = {
};
if(JSON.stringify(data) == "{}"){
}
// good
const data = {
};
if(Object.keys(data).length){
}
// bad
const data = [];
if(JSON.stringify(data) == "[]"){
}
// good
const data = [];
if(data.length){
}
// bad
function test() {
console.log('test1');
};
function test2() {
console.log('test2');
}
var test3 = 1;
if(test3 === 1){
test1();
}else{
test2();
}
// good
(test3 === 1 ? test1 : test2)();
// bad
switch (data) {
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test3();
break;
}
// good
var data = {
1: test1,
2: test2,
3: test
};
data[1] && data[1]();
data[2] && data[2]();
data[3] && data[3]();
// bad
const data = 'abc abc abc abc abc abc\n\t'
+ 'test test,test test test test\n\t'
// good
const data = `abc abc abc abc abc abc
test test,test test test test`
// bad
function fn(a) {
return Math.PI * a;
}
// good
const fn = a => (
Math.PI * a
)
// bad
let str = '';
for(let i = 0; i < 5; i++) {
str += 'test';
}
// good
'test '.repeat(5);
// bad
Math.pow(2, 3);
// good
2**3
// bad
const data = {
a: 1, b: 2, c: 3 };
const arr = [];
for(const key in data){
arr.push([key, data[key]]);
};
console.log(arr);
// good
const data = {
test1: 'abc', test2: 'cde', test3: 'efg' };
const arr = Object.entries(data);
console.log(arr);
// 输出
[ [ 'a', 1 ],
[ 'b', 2 ],
[ 'c', 3 ] ]
(两个按位非运算符只适用于 32 位整型)
// bad
Math.floor(1.9) === 1;
// good
~~1.9 === 1
// bad
if(Boolean([].length)){
}
// good
if(!![].length){
}
可选链操作符?.的作用就是判断这个对象(res)下的(res.data)下的(res.data.list)是否为null或者undefined,当其中一链为null或者undefined时就返回undefined,这样即使中间缺少一个属性也不会报错
// bad
let arr = res && res.data && res.data.list
// good
let arr = res?.data?.list
// bad
let arr = res && res.data || []
// good
let arr = res?.data ?? []
||: 判断布尔值为false时,赋默认值
??:只判断null和undefined时,赋默认值
console.log(1 || "xx") //1
console.log(0 || "xx") //xx
console.log(null || "xx") //xx
console.log(undefined || "xx") //xx
console.log(-1 || "xx") //-1
console.log("" || "xx") //xx
console.log(1 ?? "xx") //1
console.log(0 ?? "xx") //0
console.log(null ?? "xx") //xx
console.log(undefined ?? "xx") //xx
console.log(-1 ?? "xx") //-1
console.log("" ?? "xx") //''
// bad
let arr = [];
for(const key in {
key1: 1, key2: 2}){
arr.push(key)
}
console.log(arr); // ["key1", "key2"]
// good
console.log(Object.keys({
key1: 1, key2: 2})); // ["key1", "key2"]
// bad
let arr = [];
const obj = {
key1: 1, key2: 2};
for(const key in obj ){
arr.push(obj[key])
}
console.log(arr); // [1, 2]
// good
console.log(Object.values({
key1: 1, key2: 2})); // [1, 2]