
这里列出在项目中常用的ES6相关特性,以便更快的理解和应用ES6。
常用特性
- 作用域控制 let、const
- 模板字符串
- 语法糖 - 箭头函数
- 解构
- 类与模块
- Promise
let & const
const
不可重新赋值的值 (常量、配置项以及引用的组件)
let
使用let声明的变量只在语句块内有效
let
的使用场景相对较少的,我们只会在 loop(for,while 循环)及少量必须重定义的变量上用到他
let 的使用场景
1 2 3 4 5 6 7 8 9 10 11 12 13
| function f1() { let n = 5; if (true) { let n = 10; } console.log(n); } for (let i = 0; i < buttons.length; i++) { }
|
const
由于不可以重新赋值的特性,所以可以做更多语法静态分析方面的优化,从而有更高的执行效率。
const 的使用场景
1 2 3 4 5 6 7 8 9 10 11
| const REG_GET_INPUT = /^\d{1,3}$/; const config = { isDev : false, pubDir: './admin/' } const gulp = require('gulp');
|
模板字符串 Template Strings
增强版的字符串,用反引号(`)标识,支持变量注入与多行文本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| const start = 'hi all'; const getName = () => { return 'jelly'; }; const conf = { fav: 'Coding' }; const msg = `${start}, my name is ${getName()}, ${conf.fav} is my favourite`; const wantToSay = `I'm a "tbfed"`; const slogan = ` I have a dream today! `; const resultTpl = ` <section> <div>...</div> </section> `;
|
箭头函数 Arrow Function
使用箭头(=>)进行定义的函数,属于匿名函数(Lambda)一类
箭头函数没有独立执行上下文( this ),所以其内部引用 this 对象会直接访问父级。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| const getOptions = (name, key) => { ... } const getOptions = key => { ... } const getOptions = key => console.log(key); const noop = () => {}; let names = [ 'Will', 'Jack', 'Peter', 'Steve', 'John', 'Hugo', 'Mike' ] let newSet = names .map((name, index) => { return { id: index, name: name } }) .filter(man => man.id % 2 == 0) .map(man => [man.name]) .reduce((a, b) => a.concat(b)) console.log(newSet)
|
解构 Destructuring
用于分解方法的参数、数组、对象中的变量
1 2 3 4 5 6 7 8 9 10 11 12 13
| const bookSet = ['UED', 'TB fed', 'Not find']; const bookCollection = () => { return {book1: 'UED', book2: 'TB fed'}; }; const {book1, book3 = 'Not find'} = bookCollection(); const [book1,,book3] = bookSet; const {length: setLength} = bookSet;
|
Rest运算符(解构赋值)/ Spread扩展运算符(…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const getOptions = function(...args){ console.log(args.join); }; const getOptions = (...args) => { console.log(args); }; const [opt1, ...opts] = ['one', 'two', 'three', 'four']; const opts = ['one', 'two', 'three', 'four']; const config = ['other', ...opts];
|
类与模块 Class & Modules
class
定义一个类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Point { constructor(x, y) { this.x = x; this.y = y; } get prop() { return 'getter'; } set prop(value) { console.log('setter: '+value); } toString() { return '(' + this.x + ', ' + this.y + ')'; } static classMethod() { return 'hello'; } static get HuaChen(){ return 'jelly'; } }
|
使用 extend
关键字实现类的继承
1 2 3 4 5 6 7 8 9 10
| class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return this.color + ' ' + super.toString(); } }
|
import
模块引入的方式
1 2 3 4 5 6 7 8 9 10
| import name from "module-name" import * as name from "module-name" import { member } from "module-name" import { member as alias } from "module-name" import { member1 , member2 } from "module-name" import { member1 , member2 as alias2 , [...] } from "module-name" import defaultMember, { member [ , [...] ] } from "module-name" import defaultMember, * as alias from "module-name" import defaultMember from "module-name" import "module-name"
|
export
模块导出或对外提供接口的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| export var firstName = 'Michael'; export var lastName = 'Jackson'; export var year = 1958; var firstName = 'Michael'; var lastName = 'Jackson'; var year = 1958; export {firstName, lastName, year}; export default function crc32() { } import crc32 from 'crc32'; export function crc32() { }; import {crc32} from 'crc32'; function add(x, y) { return x * y; } export {add as default};
|
export default
命令其实只是输出一个叫做 default
的变量,所以它后面不能跟变量声明语句。
1 2 3 4 5 6 7 8 9
| export var a = 1; var a = 1; export default a; export default var a = 1;
|
Promise
Promise 为异步编程提供统一的解决方案,比传统的回调和事件更加合理有效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| Promise .all([jsBuildPromise, cssBuildPromise]) .then(() => { ... }); gitPromise .then(() => git.add()) .then(() => { git.commit(); }) .then(() => { return git.log(); }); new Promise(() => { f; }) .catch((err) => { console.log(err) }); console.log('error test');
|