@Secretmm
2021-01-21T02:30:25.000000Z
字数 3665
阅读 456
梳理
let
和const
,不使用var
,优先使用const
// 推荐
const a = 1
let b = 2
// 不推荐
var a = 1
var b = 2
const
和let
分组
const b = 1;
const d = 2;
let a;
let c;
let e;
// 不推荐
let a = b = c = 1;
new Object()
// 推荐
const a = {};
// 不推荐
const a = new Object();
// 推荐
const item = {
value: 1,
addValue(val) {
return item.value + val;
},
};
// 不推荐
const item = {
value: 1,
addValue: function(val) {
return item.value + val;
},
};
const job = 'FrontEnd';
// 推荐
const item = {
job,
};
// 不推荐
const item = {
job: job,
};
const job = 'FrontEnd';
const company = 'daddylab';
// 推荐
const item = {
job,
company,
sex: 'male',
age: 25,
};
// 不推荐
const item = {
sex: 'male',
job,
age: 25,
company,
};
// 推荐
const good = {
foo: 3,
bar: 4,
'data-blah': 5,
};
// 不推荐
const bad = {
'foo': 3,
'bar': 4,
'data-blah': 5,
};
...
来浅拷贝对象
// 推荐
const original = { a: 1, b: 2 };
const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
// 不推荐
const original = { a: 1, b: 2 };
const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }
// 推荐
const items = [];
// 不推荐
const items = new Array();
push
方法向数组中添加元素
const items = [];
// 推荐
items.push('test');
// 不推荐
items[items.length] = 'test';
...
复制数组
// 推荐
const itemsCopy = [...items];
// 不推荐
const itemsCopy = [];
for (i = 0; i < items.length; i++) {
itemsCopy[i] = items[i];
}
// 推荐
const arr = [[0, 1], [2, 3], [4, 5]];
const arr2 = [
[0, 1],
[2, 3],
[4, 5]
];
const objectInArray = [
{
id: 1,
},
{
id: 2,
}
];
const numberInArray = [
1,
2
];
// 不推荐
const arr = [
[0, 1], [2, 3], [4, 5]
];
const objectInArray = [{
id: 1,
}, {
id: 2,
}];
const numberInArray = [
1, 2
];
// 推荐
function getFullName(user) {
const { firstName, lastName } = user;
return `${firstName} ${lastName}`;
}
// 很推荐
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
// 不推荐
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
return `${firstName} ${lastName}`;
}
const arr = [1, 2, 3, 4];
// 推荐
const [first, second] = arr;
// 不推荐
const first = arr[0];
const second = arr[1];
// 推荐
function doSomething() {
return { top, right, bottom, left };
}
const { top, left } = doSomething();
// 不推荐
function doSomething() {
return [top, right, bottom, left];
}
const [top, xx, xxx, left] = doSomething();
const test = 'test';
// 推荐
const str = `ab${test}`;
// 不推荐
const str = 'a' + 'b' + test;
eval()
(function() {
// do something
}());
...
,而不是arguments
// 推荐
function test(...args) {
return args.join('');
}
// 不推荐
function test() {
const args = Array.prototype.slice.call(arguments);
return args.join('');
}
// 推荐
function handleThings(opts = {}) {
// ...
}
// 不推荐
function handleThings(opts) {
opts = opts || {};
// ...
}
let b = 1
// 不推荐
function count(a = b++) {
console.log(a);
}
// 推荐
function handleThings(name, opts = {}) {
// ...
}
// 不推荐
function handleThings(opts = {}, name) {
// ...
}
// 不推荐
function f1(obj) {
obj.key = 1;
}
function f1(a) {
a = 1;
}
// 推荐
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
// 不推荐
[1, 2, 3].map(function(x) {
const y = x + 1;
return x * y;
});
return
// 推荐
[1, 2, 3].map(number => `A string containing the ${number}.`);
// 推荐
[1, 2, 3].map(x => x * x);
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
class
,避免直接操作prototype
extends
来实现继承
// 不推荐
class Foo {
bar () { return 1 }
bar () { return 2 }
}
区分Object
和Map
,只有模拟现实世界的实体对象时,才使用Object
。如果只是需要key: value
的数据结构,使用Map
结构
ES6
模块语法的场合使用ES6
模块语法import
和export
,而不是CommonJs语法的require
import
一次,有多个import
请写在一起export default
与export
不要同时使用
// 推荐
import foo, {
named1,
named2
} from 'foo';
// 不推荐
import foo from 'foo';
// … some other imports … //
import { named1, named2 } from 'foo';
import
语句放在文件最前方.
来访问对象属性,当访问的属性是变量时才使用[]
// 推荐
const name = joke.name;
function getProp (prop) {
return joke[prop];
}
// 不推荐
const name = joke['name'];
使用===
和!==
而非==
和!=
一般情况禁用该方法
禁止修改内置对象,如Object
和Array