front-end-interview-handbook/packages/quiz/questions/how-can-you-share-code-betw.../zh-CN.mdx

12 lines
615 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 如何在文件之间共享代码?
---
这取决于 JavaScript 环境。
在客户端上(浏览器环境),只要变量/函数在全局范围(`window`) 内声明,所有脚本都可以引用它们。 另一种办法是通过 RequireJS 采用异步模块定义AMD以获取更多模块化方法。
在服务器 (Node.js) 上,通用的方法是使用 CommonJS。 每个文件被当作模块处理,它可以将变量和函数导出到`module.exports`对象。
ES2015 定义了一个模块语法,旨在替换 AMD 和 CommonJS。 这最终将在浏览器和 Nodejs 环境中得到支持。