我试过这个:
// mod.js
var a = 1;
this.b = 2;
exports.c = 3;
// test.js
var mod = require('./mod.js');
console.log(mod.a); // undefined
console.log(mod.b); // 2
console.log(mod.c); // 3, so this === exports?
因此,我设想require()可以像这样实现:
var require = function (file) {
var exports = {};
var run = function (file) {
// include "file" here and run
};
run.apply(exports, [file]);
return exports;
}
是那么回事吗?请帮我理解一下require(),或者在哪里可以找到源码。谢谢!
https://stackoverflow.com/questions/9475792
复制相似问题