Beace Lee

Beace Blog

Written by Beace Lee who lives and works in China building useful things. You should follow him on Twitter

ECMAScript 2015 Features-c07-modules

February 21, 2017

ECMAScript 2015 Features

Modules

JavaScript遵循着两种比较流行的模块加载器——AMDCommonJS模式。在未引用该模块之前,该模块不执行,知道被import

写法

//utils.js
export function sum(x, y) {
  return x + y;
}
export const pi = Math.PI;
//app.js
import {sum, pi} from "./utils/js";
console.log(sum(pi + pi)); //6.283185307179586

OR

//app.js
import * as Utils from "./utils.js";
console.log(Utils.sum(Utils.pi + Utils.pi)); ////6.283185307179586