ES6 module

Overview

  • A preference for single exports and support for cyclic dependencies.
  • Support asynchronous loading and configurable module loading.
  • Bundle structure could be statically analyzed for static checking, optimization etc.

The ES6 module standard has two parts:
- Declarative syntax for importing and exporting.
- Programmatic loader API: to configure how modules are loaded and to conditionally load modules.

Module Syntax

There are two kinds of exports: named exports (several per module) and default exports (one per module)

Named Exports

A module can export multiple things with the key word export and distinguished by their names .

// lib.js
export const constant = 'constant'
function funcA () {}
function funcB () {}
export { funcA, funcB }

// main.js
import { constant, funcA, funcB } from 'lib.js'
// or you could import everything
import * as lib from 'lib.js'
Default Exports

ES6 allows to export a default value for one module (file), and then import it with a name.

// lib.js
export default function () {}
// main.js
import Func from 'lib.js'
Mixing named exports and default export in a module

It’s a common usage to have both named exports and default export in a module in the case that we want to export a standard interface as default export and also exports multiple specific values in the meanwhile.

// lib.js
function funcA () {}
function funcB () {}
let all = { funcA: funcA, funcB: funcB }
export default all
export { funcA, funcB }

// main.js
import all, { funcB } from 'lib.js'

Notice that the default export is actually a named export with the special name default.

import { default as all } from 'lib.js'
// equivalent to 
import all from 'lib.js'

const constant = 'constant'
export default constant
// equivalent to
export { constant as default }

Advantages

Static Module Structure

ES6 enforces a static structure, which means import and export must be determined at compile time. You can not import modules conditionally like if...else....

Synchronous And Asynchronous Loading

Since ES6 enforces static module structure, by default modules are synchronous loaded and executed. Conditional importing and asynchronous resolving (it means using import directive here) is prohibited. ES6 provides import() method for asynchronous loading and resolving, which return a promise for afterwards snippets when it’s loaded and resolved.

Support For Cyclic Dependencies

ES6 modules export bindings, not values, and therefore connection to variables declared inside module body keeps alive. This is the point quite different from commonJS, which return the instance (value) of the exporting module and every time you import it, it remains the values declared inside the module body though you change it in functions.

Circular dependencies

Circular dependencies is that importing each other in files, which is the different situation from cyclic dependencies. You might find it not work the way you want if you run across circular dependencies.

// moduleA.js
import { b } from 'moduleB.js'
console.log('b: ' + b)
const a = 'a'
console.log('module A executed')
export { a }

// moduleB.js
import { a } from 'moduleA.js'
console.log('a: ' + a)
const b = 'b'
console.log('module B executed')
export { b }

// entry main.js
import { b } from 'moduleB.js'

// b: undefined (Error Reference)
// module A executed
// a: a
// module B executed
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ES6模块和CommonJS的区别在于它们的语法和实现方式不同。ES6模块是ES6规范中定义的一种模块化方式,它使用import和export关键字来导入和导出模块。而CommonJS是Node.js中使用的一种模块化方式,它使用require和module.exports来导入和导出模块。 ES6模块支持静态分析,可以在编译时确定模块的依赖关系,从而实现更好的性能和可靠性。而CommonJS模块是动态加载的,需要在运行时才能确定模块的依赖关系,因此在性能和可靠性方面不如ES6模块。 此外,ES6模块还支持命名导入和导出,可以更灵活地控制模块的导入和导出。而CommonJS模块只支持默认导入和导出,无法实现命名导入和导出。 ### 回答2: ES6 module和CommonJS是两种不同的模块化解决方案,虽然两者主要目的都是为了让JavaScript代码更容易组织和重用,但是它们在具体实现上存在很大的区别,本文将从以下四个方面来介绍它们的区别: 1.语法区别 ES6 module使用了新的关键字来声明模块中的变量、方法和类等例如export、import等,而CommonJS则用require()和module.exports。ES6 module的语法更加简洁易读且与JavaScript同步加载,CommonJS的语法比较复杂,且需要异步加载模块。 2.模块加载方式不同 ES6模块在编译的时候会根据import引用的模块信息,去异步加载依赖的模块,这种方式叫做静态加载。而CommonJS的模块则是在运行时同步加载,也就是说CommonJS会将所有依赖的模块全部加载完成,然后再运行代码,这种方式比较适合服务器端的开发。 3.ES6模块有作用域 ES6模块和CommonJS中的模块是有作用域的,但是ES6模块中的作用域是静态的,而CommonJS中的模块作用域是动态的,也就是说在CommonJS中,模块中变量的值在代码运行期间是可变的,而在ES6模块中则不是。 4.ES6模块支持循环依赖 ES6模块中是支持循环依赖的,它会先给所有需要依赖的模块都分配一个空间,然后再去填充它们,这样就避免了循环依赖的问题。而CommonJS中的模块是不支持循环依赖的,因为在CommonJS中,模块的加载是同步的,所以不能把某个模块的加载工作放在另外一个模块加载完毕之后再执行。 总之,ES6 module是一种新的、更加先进和可靠的模块化方案,并且支持静态加载和循环依赖等功能,虽然目前不被所有浏览器支持,但是它的优点是显而易见的。相比之下,CommonJS更适用于服务器端的开发,但是因为它还需要异步加载和模块作用域的问题等,所以在实际的开发过程中并不是很方便和实用。 ### 回答3: ES6模块和CommonJS模块是JavaScript中两种主要的模块化规范,它们有一些明显的区别。 1. 语法不同 ES6模块的导入和导出是基于语法的,也就是使用import和export关键字来操作。而CommonJS模块则是使用require()函数来导入模块,使用module.exports(或exports的简写)来导出模块。 2. 执行方式不同 ES6模块是在代码编译时进行解析的,也就是在代码执行前进行模块解析和编译。而CommonJS模块是在代码运行时才会进行解析。 3. 是否支持静态分析 ES6模块支持静态分析,也就是可以在编译时对模块进行分析和优化,这在性能上有很大的优势。而CommonJS模块不支持静态分析,这在性能上有些影响。 4. 值拷贝与动态绑定不同 在ES6模块中,导入的变量是值拷贝,也就是说,导入的变量与导出的变量不是同一个引用,它们是两个相互独立的对象。而在CommonJS模块中,导入的变量是动态绑定,也就是说,导入的变量与导出的变量是同一个引用。这可能会导致意外的副作用。 5. 是否支持异步加载 ES6模块原生支持异步加载,也就是可以在代码中使用import()函数来异步加载模块。而CommonJS模块不支持异步加载,必须在同步代码中使用require()来加载模块。 总体来说,ES6模块在语法、执行方式、静态分析、值拷贝与动态绑定、支持异步加载等方面都有优势,这也是为什么越来越多的JavaScript开发者选择使用ES6模块。不过,在Node.js环境下,CommonJS模块仍然是主要的模块化规范。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值