ES6 require和import的区别

目录

1、ES6 模块的设计思想

2、ES6 模块默认使用严格模式

3、export

4、import

5、module 的整体加载

6、export default

7、import 和 require 的对比

8、import()函数适用场合

(1)、按需加载

(2)、条件加载


我们经常看到在 js 文件中出现 require,还有 import,这两个都是为了JS 模块化编程使用。CSS 的是 “@import” 。

1、ES6 模块的设计思想

ES6 模块的设计思想,是尽量的静态化,使得编译时就能确定模块的依赖关系,以及输入和输出的变量。

// CommonJS模块
let { stat, exists, readFile } = require('fs');

// 等同于
let _fs = require('fs');
let stat = _fs.stat;
let exists = _fs.exists;
let readfile = _fs.readfile;

above:整体加载 fs 模块(即加载 fs 所有方法),生成一个对象"_fs",然后再从这个对象上读取三个方法,这叫“运行时加载”,因为只有运行时才能得到这个对象,不能在编译时做到静态化。

ES6 模块不是对象,而是通过 export 命令显示指定输出代码,再通过 import 输入。

import { stat, exists, readFile } from 'fs';

above:从 fs 加载 “stat, exists, readFile” 三个方法,其他方法不加载。

2、ES6 模块默认使用严格模式

ES6 模块默认使用严格模式,无论是否声明 “use strict”。所以,顶层的 this 指向 undefined,即不应该在顶层代码使用 this

Module 主要由两个命令组成,import和export,export用于规定模块的对外接口,import命令用于输入其他模块提供的功能

3、export

模块是独立的文件,该文件内部的所有的变量外部都无法获取。如果希望获取某个变量,必须通过export 输出。

// profile.js
export var firstName = 'Michael';
export var lastName = 'Jackson';
export var year = 1958;

或者用更好的方式——用大括号指定要输出的一组变量:

// profile.js
var firstName = 'Michael';
var lastName = 'Jackson';
var year = 1958;

export {firstName, lastName, year};

除了输出变量,还可以输出函数或者类(class):

export function multiply(x, y) {
  return x * y;
};

还可以批量输出,同样是要包含在大括号里,也可以用as重命名:

function v1() { ... }
function v2() { ... }

export {
  v1 as streamV1,
  v2 as streamV2,
  v2 as streamLatestVersion
};

【注意】:

  • export 命令规定的是对外接口,必须与模块内部变量建立一一对应的关系
// 写法一
export var m = 1;

// 写法二
var m = 1;
export {m};

// 写法三
var n = 1;
export {n as m};


// 报错
export 1;

// 报错
var m = 1;
export m;

报错的写法原因是:没有提供对外的接口,第一种直接输出1,第二种虽然有变量m,但还是直接输出1,导致无法解构。

  • 同样的,function 和 class的输出,也必须遵守这样的写法。
// 报错
function f() {}
export f;

// 正确
export function f() {};

// 正确
function f() {}
export {f};
  • export语句输出的接口,都是和其对应的值是动态绑定的关系,即通过该接口取到的都是模块内部实时的值。
  • export模块可以位于模块中的任何位置,但是必须是在模块顶层,如果在其他作用域内,会报错。
function foo() {
  export default 'bar' // SyntaxError
}
foo()

4、import

export 定义了模块的对外接口后,其他 JS 文件就可以通过 import 来加载这个模块。

// main.js
import {firstName, lastName, year} from './profile';

function setName(element) {
  element.textContent = firstName + ' ' + lastName;
}

import 命令接受一对大括号,里面指定要从其他模块导入的变量名,必须与被导入模块(profile.js)对外接口的名称相同

如果想重新给导入的变量一个名字,可以用 as 关键字

import { lastName as surname } from './profile';

import 后的 from 可以指定需要导入模块的路径名,可以是绝对路径,也可以是相对路径, 其中 js 文件类型的 “.js” 文件名后缀可以省略,如果只有模块名,不带有路径,需要有配置文件指定。

【注意】:

  • import命令具有提升效果,会提升到整个模块的头部,首先执行。(是在编译阶段执行的)

因为import是静态执行的,不能使用表达式和变量,即在运行时才能拿到结果的语法结构(e.g. if...else...)

5、module 的整体加载

除了指定加载某个输出值,还可以用(*)指定一个对象,所有的变量都会加载在这个对象上。

// circle.js 输出两个函数

export function area(radius) {
  return Math.PI * radius * radius;
}

export function circumference(radius) {
  return 2 * Math.PI * radius;
}
// main.js 加载在个模块

import { area, circumference } from './circle';

console.log('圆面积:' + area(4));
console.log('圆周长:' + circumference(14));

//上面写法是逐一指定要加载的方法,整体加载的写法如下。
import * as circle from './circle';

console.log('圆面积:' + circle.area(4));
console.log('圆周长:' + circle.circumference(14));

注意,模块整体加载所在的那个对象(上例是 circle),应该是可以静态分析的,所以不允许运行时改变。

import * as circle from './circle';

// 下面两行都是不允许的
circle.foo = 'hello';
circle.area = function () {};

6、export default

之前的例子中,使用 import 导入时,都需要知道模块中所要加载的变量名或函数名,用户可能不想阅读源码,只想直接使用接口,就可以用 export default 命令,为模块指定输出

// export-default.js
export default function () {
  console.log('foo');
}

其他模块加载该模块时,import 命令可以为该匿名函数指定任意名字。

// import-default.js
import customName from './export-default';
customName(); // 'foo'

export default 也可以用于非匿名函数前。

下面比较一下默认输出和正常输出。

// 第一组
export default function crc32() { // 输出
  // ...
}

import crc32 from 'crc32'; // 输入

// 第二组
export function crc32() { // 输出
  // ...
};

import {crc32} from 'crc32'; // 输入

可以看出,使用 export default 时,import 语句不用使用大括号。

7、import 和 require 的对比

  • import 和 export 命令只能在模块的顶层,不能在代码块之中。否则会语法报错。这样的设计,可以提高编译器效率,但是没有办法实现运行时加载。所以引入了 import()函数,来支持动态加载。
  • require() 函数是运行时加载的,支持动态加载功能。
import(specifier)

specifier 用来指定所要加载的模块的位置。import 能接受什么参数,import() 可以接受同样的参数。

import() 返回一个 Promise 对象。

const main = document.querySelector('main');

import(`./section-modules/${someVariable}.js`)
  .then(module => {
    module.loadPageInto(main);
  })
  .catch(err => {
    main.textContent = err.message;
  });

8、import()函数适用场合

(1)、按需加载

button.addEventListener('click', event => {
  import('./dialogBox.js')
  .then(dialogBox => {
    dialogBox.open();
  })
  .catch(error => {
    /* Error handling */
  })
});

最重要的是:import 模块在事件监听函数中,只有用户点击了按钮,才会加载这个模块

(2)、条件加载

import() 函数可以放在 if...else 语句中,实现条件加载。

if (condition) {
  import('moduleA').then(...);
} else {
  import('moduleB').then(...);
}

---摘自阮大神的 ES6, 仅供自己记录参考。

【参考】

JS 中的require 和 import 区别https://www.cnblogs.com/liaojie970/p/7376682.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值