CMJ
文件是一个模块,公有,内置两个变量 module require (exports = module.exports);
一个引入 一个导出,就形成了通信的根本构造
须要留神两个问题
-
缓存,require 会缓存一下,所以
// a.js var name = 'morrain'; var age = 18; exports.name = name; exports.getAge = function(){return age;} // b.js var a = require('a.js'); console.log(a.name); // 'morrain' a.name = 'rename'; var b = require('a.js'); console.log(b.name) // 'rename'
- 援用拷贝还是值拷贝 (CMJ 是值拷贝)
// a.js
var name = 'morrain';
var age = 18;
exports.name = name;
exports.age = age;
exports.setAge = function(a){age = a;}
// b.js
var a = require('a.js');
console.log(a.age); // '18'
a.setAge(19);
console.log(a.age) // '18'
- 运行时加载 / 执行时加载(多阶段,异步)ESM