CommonJS
一、规范
1.1 说明
- http://wiki.commonjs.org/wiki/Modules/1.1
- 每个文件都可当作一个模块
- 在服务器端: 模块的加载是运行时同步加载的
- 在浏览器端: 模块需要提前编译打包处理
1.2 基本语法
- 暴露模块
module.exports = value
exports.xxx = value
- 问题: 暴露的模块到底是什么?
- 引入模块
- require(xxx)
- 第三方模块:xxx为模块名
- 自定义模块: xxx为模块文件路径
- require(xxx)
二、实现
2.1 基于服务端(Node)实现
Node.js官方文档:http://nodejs.cn/
- 下载安装node.js
- 创建项目结构
|-modules
|-module1.js
|-module2.js
|-module3.js
|-app.js
|-package.json
{
"name": "commonJS-node",
"version": "1.0.0"
}
其中<