关于前端:ES6模块与CommonJS模块有什么区别呢

4次阅读

共计 1174 个字符,预计需要花费 3 分钟才能阅读完成。

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》

正文完
 0