记录我的第一天部署的博客~这是最早的一篇文章!
记录一点指令,怕忘记啦
今天记录一下es6的语法规范
语法规范
使用数组成员对变量赋值时,优先使用解构赋值
1 2 3 4 5 6 7 8
| const arr = [1, 2, 3, 4];
const first = arr[0]; const second = arr[1];
const [first, second] = arr;
|
如果是函数的话
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function getFullName(user) { const firstName = user.firstName; const lastName = user.lastName; }
function getFullName(obj) { const { firstName, lastName } = obj; }
function getFullName({ firstName, lastName }) { }
|
如果函数返回多个值,优先使用对象的解构赋值,而不是数组的解构赋值。这样便于以后添加返回值,以及更改返回值的顺序。
1 2 3 4 5 6 7 8 9 10 11
| function processInput(input) { return [left, right, top, bottom]; }
function processInput(input) { return { left, right, top, bottom }; }
const { left, right } = processInput(input);
|
数组
1 2 3 4 5 6 7 8 9 10 11
| const len = items.length; const itemsCopy = []; let i;
for (i = 0; i < len; i++) { itemsCopy[i] = items[i]; }
const itemsCopy = [...items];
|
那些使用匿名函数当作参数的场合,尽量用箭头函数代替。因为这样更简洁,而且绑定了 this。
1 2 3 4 5 6 7 8 9 10 11 12
| [1, 2, 3].map(function (x) { return x * x; });
[1, 2, 3].map((x) => { return x * x; });
[1, 2, 3].map(x => x * x);
|
模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const moduleA = require('moduleA'); const func1 = moduleA.func1; const func2 = moduleA.func2;
import { func1, func2 } from 'moduleA';
function makeStyleGuide() { }
export default makeStyleGuide;
const StyleGuide = { es6: { } };
export default StyleGuide;
|