假设有模块counter.js:
var i = 0;
function count() {
return ++i;
}
exports.count = count;
主模块main.js:
var counter1 = require('./util/counter');
var counter2 = require('./util/counter');
console.log(counter1.count());
console.log(counter2.count());
console.log(counter2.count());
/**
* 运行结果:
1
2
3
* /
由此说明,虽然模块被多次引入,但只会初始化一次。