es6模块化语法

ES6模块化语法更加简洁,我们直接看示例吧。

简洁

如果只是输出唯一的对象,使用export default即可,代码如下:

// 创建 util1.js 文件,内容加
export default {
  a: 100
}

// 创建 index.js 文件,内容加
import obj from './util1.js'
console.log(obj)

如果要输出多个对象,就不能使用default了,且import的时候要加{…},代码如下:

// 创建 util2.js 文件,内容加
export function fn1() {
  alert('fn1')
}
export function fn2() {
  alert('fn2')
}

// 创建 index.js 文件,内容加
import { fn1, fn2 } from './util2.js'
fn1()
fn2()