ES6

167 阅读1分钟

Block-Scoped Constructs Let and Const(块作用域和构造 let 和 const)

Arrow Functions in(箭头函数) ES6

Default Parameters(默认参数) in ES6

var link = function(height = 50, color = 'red', url = 'http://aaa.co') {

  ...

}

Template Literals(模板对象) in ES6

var name = `Your name is ${first} ${last}. `;

var url = `http://localhost:3000/api/messages/${id}`;

Multi-line Strings (多行字符串)in ES6

var roadPoem = `Then took the other, as just as fair,

    And having perhaps the better claim

    Because it was grassy and wanted wear,

    Though as for that the passing there

    Had worn them really about the same,`;

var fourAgreements = `You have the right to be you.

    You can only be you when you do your best.`;

Destructuring Assignment (解构赋值)in ES6

var { house, mouse} = $('body').data(); 

var {jsonMiddleware} = require('body-parser');

var {username, password} = req.body;

Promise in ES6

Classes (类)in ES6

Modules (模块)in ES6

// ES6模块
import { stat, exists, readFile } from 'fs';

只加载3个方法,其他方法不加载,即 ES6 可以在编译时就完成模块加载。
  • CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用。
  • CommonJS 模块是运行时加载,ES6 模块是编译时输出接口。
  • CommonJS 模块的require()是同步加载模块,ES6 模块的import命令是异步加载,有一个独立的模块依赖的解析阶段。
  • CommonJS 加载的是一个对象(即module.exports属性),该对象只有在脚本运行完才会生成。而 ES6 模块不是对象,它的对外接口只是一种静态定义,在代码静态解析阶段就会生成。