CommonJS 模块及模块引用
1 2 3 4
| module.exports = function( value ){ return value * 2; }
|
1 2 3
| var multiplyBy2 = require('./moduleA'); var result = multiplyBy2(4);
|
ES6 模块及模块引用
1 2 3 4
| export function multiplyBy2( value ) { return value * 2; }
|
1 2 3
| import multiplyBy2 from './moduleA.js'; var result = multiplyBy2(4);
|
CommonJS和ES6的模块的区别:
规范 |
暴露模块 |
引入模块 |
CommonJS |
module.exports |
require(‘..’) |
ES6 |
export |
import A from ‘..’ |
一个ES6的暴露类和引用类的栗子:
1 2 3 4 5
| export default class Action{ getter: function(){} setter: function(){} }
|
1 2 3 4
| import Action from ./moduleA.js;
var action = new Action();
|