使用 ES6/7 的新特性

注:摘抄自dva.js 知识导图

变量声明

const 和 let

不要用 var,而是用 constlet,分别表示常量和变量。不同于 var 的函数作用域,constlet 都是块级作用域。

1
2
3
4
const DELAY = 1000;

let count = 0;
count = count + 1;

默认参数

1
2
3
4
5
function logActivity(activity = 'skiing') {
console.log(activity);
}

logActivity(); // skiing

模板字符串

模板字符串提供了另一种做字符串组合的方法。

1
2
3
4
5
6
7
8
const user = 'world';
console.log(`hello ${user}`); // hello world

// 多行
const content = `
Hello ${firstName},
Thanks for ordering ${qty} tickets to ${event}.
`;

箭头函数

函数的快捷写法,不需要通过 function 关键字创建函数,并且还可以省略 return 关键字。

同时,箭头函数还会继承当前上下文的 this 关键字。

比如:

1
[1, 2, 3].map(x => x + 1);  // [2, 3, 4]

等同于:

1
2
3
[1, 2, 3].map((function(x) {
return x + 1;
}).bind(this));

模块的 Import 和 Export

import 用于引入模块,export 用于导出模块。

比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 引入全部
import React from 'react';

// 引入部分
import { Component } from 'react';
import { Row, Col } from 'antd';

// 引入全部并作为 github 对象
import * as github from './services/github';

// 导出默认
export default App;

// 部分导出,需 import { App } from './file'; 引入
export class App extend Component {};

ES6 对象和数组

析构赋值

析构赋值让我们从 Object 或 Array 里取部分数据存为变量。

1
2
3
4
5
6
7
8
9
// 对象
const user = { name: 'guanguan', age: 2 };
const { name, age } = user;
console.log(`${name} : ${age}`); // guanguan : 2

// 数组
const arr = [1, 2];
const [foo, bar] = arr;
console.log(foo); // 1

我们也可以析构传入的函数参数。

1
2
3
const add = (state, { payload }) => {
return state.concat(payload);
};

析构时还可以配 alias,让代码更具有语义。

1
2
3
const add = (state, { payload: todo }) => {
return state.concat(todo);
};

对象字面量改进

这是析构的反向操作,用于重新组织一个 Object 。

1
2
3
4
const name = 'duoduo';
const age = 8;

const user = { name, age }; // { name: 'duoduo', age: 8 }

定义对象方法时,还可以省去 function 关键字。

1
2
3
4
5
6
7
8
app.model({
reducers: {
add() {} // 等同于 add: function() {}
},
effects: {
*addRemote() {} // 等同于 addRemote: function*() {}
},
});

Spread Operator

Spread Operator 即 3 个点 ...,有几种不同的使用方法。

可用于组装数组。

1
2
const todos = ['Learn dva'];
[...todos, 'Learn antd']; // ['Learn dva', 'Learn antd']

也可用于获取数组的部分项。

1
2
3
4
5
6
7
const arr = ['a', 'b', 'c'];
const [first, ...rest] = arr;
rest; // ['b', 'c']

// With ignore
const [first, , ...rest] = arr;
rest; // ['c']

还可收集函数参数为数组。

1
2
3
4
function directions(first, ...rest) {
console.log(rest);
}
directions('a', 'b', 'c'); // ['b', 'c'];

代替 apply。

1
2
3
4
5
6
function foo(x, y, z) {}
const args = [1,2,3];

// 下面两句效果相同
foo.apply(null, args);
foo(...args);

对于 Object 而言,用于组合成新的 Object 。(ES2017 stage-2 proposal)

1
2
3
4
5
6
7
8
9
10
11
const foo = {
a: 1,
b: 2,
};
const bar = {
b: 3,
c: 2,
};
const d = 4;

const ret = { ...foo, ...bar, d }; // { a:1, b:3, c:2, d:4 }

Promises

Promise 用于更优雅地处理异步请求。比如发起异步请求:

1
2
3
4
fetch('/api/todos')
.then(res => res.json())
.then(data => ({ data }))
.catch(err => ({ err }));

定义 Promise 。

1
2
3
4
5
6
7
8
9
const delay = (timeout) => {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
};

delay(1000).then(_ => {
console.log('executed');
});

-EOF-

坚持原创技术分享,您的支持将鼓励我继续创作!