ES6和CommonJS中模块化的区别- html中文网

ES6模块与CommonJS模块有什么区别?

1、ES6 ModuleCommonJS模块的区别:

  • CommonJS 是对模块的浅拷贝,ES6 Module 是对模块的引用,即ES6 Module只存只读,不能改变其值,具体点就是指针指向不能变,类似 const
  • import 的接口是 read-only(只读状态),不能修改其变量值。 即不能修改其变量的指针指向,但可以改变变量内部指针指向,可以对 commonJS 对重新赋值(改变指针指向),但是对 ES6 Module 赋值会编译报错。

2、ES6 ModuleCommonJS模块的共同点:

CommonJSES6 Module都可以对引入的对象进行赋值,即对对象内部属性的值进行改变。

详解请移步 《ES6模块与CommonJS模块的差异》

What is the difference between ES6 modules and CommonJS modules?

1、The difference between ES6 Module and CommonJS module:

  • CommonJS is a shallow copy of the module, ES6 Module is a reference to the module, that is, the ES6 Module is only stored as read-only, and its value cannot be changed. The specific point is that the pointer cannot be changed, similar to const
  • The import interface is read-only (read-only state), and its variable values ​​cannot be modified. That is, the pointer pointing to the variable cannot be modified, but the internal pointer pointing to the variable can be changed, and the commonJS pair can be re-assigned (change the pointer pointing), but the ES6 Module assignment will compile an error.

Common points of ES6 Module and CommonJS module:

  • Both CommonJS and ES6 Module can assign values ​​to imported objects, that is, change the value of the internal properties of the object.
For detailed explanation, please move to 《the difference between ES6 module and CommonJS module》

Damiao_Lee
10 声望1 粉丝

my name is damiao.