Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module #32

Open
DeanTG opened this issue Jun 5, 2023 · 1 comment
Open

module #32

DeanTG opened this issue Jun 5, 2023 · 1 comment

Comments

@DeanTG
Copy link
Owner

DeanTG commented Jun 5, 2023

  • CommonJS
  • ES6 Module
  • Asynchronous Module Definition(AMD)- RequireJS
  • Common Module Definition(CMD)- SeaJS
  • Universal Module Definition(UMD)
@DeanTG
Copy link
Owner Author

DeanTG commented Jun 5, 2023

CommonJS

Commonjs 是作为 Node 中模块化规范以及原生模块

特点

  • 由于使用了 Node 的 api,只能在服务端环境上运行
  • 模块编译本质上是沙箱编译,其内部代码运行无法访问当前执行上下文。但可以访问 global。
  • 原生 Module 对象,每个文件都是一个 Module 实例,所有代码都运行带模块作用域、不会污染全局作用域。
  • CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用。CommonJS 模块输出的是值的拷贝,也就是说,一旦输出一个值,模块内部的变化就影响不到这个值。
  • 使用 require 来导入一个模块,用 module.exports 来导出一个模块。通过 require 导入一个模块时,拿到的是导出模块 module.exports 对象的引用(值拷贝,浅拷贝,栈地址)。若给module.exports赋值一个新对象,那么就会破坏引用。
    所有文件加载均是运行时、同步加载,模块加载的顺序是按照其在代码中出现的顺序,在 require 的时候才去加载模块文件,加载完再接着执行。
  • 每个模块加载一次之后就会被缓存。
  • CommonJS 模块是运行时加载,ES6 模块是编译时输出接口。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant