@Secretmm
2021-01-21T02:30:25.000000Z
字数 3665
阅读 523
梳理
let和const,不使用var,优先使用const
// 推荐const a = 1let b = 2// 不推荐var a = 1var 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,避免直接操作prototypeextends来实现继承
// 不推荐class Foo {bar () { return 1 }bar () { return 2 }}
区分Object和Map,只有模拟现实世界的实体对象时,才使用Object。如果只是需要key: value的数据结构,使用Map结构
ES6模块语法的场合使用ES6模块语法import和export,而不是CommonJs语法的requireimport一次,有多个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