module
in 与 infile1.js
是不同的实例file2.js
。
文件1.js
module.exports.foo = function(obj) {
module.exports.obj = obj;
return module;
}
文件2.js
console.log("running file2");
file1 = require("./file1");
var something = {a: "a"};
var moduleOfFile1 = file1.foo(something); //export "something" being a part of file2
console.log("file1 module: " + moduleOfFile1.id);
console.log("file2 module: " + module.id);
安慰:
node file2.js
id
返回的不一样
更新
或者,为什么不更新 file2.js 来扩展它module.exports
文件2.js
// well, you may copy `extend` implementation if you
// do not want to depend on `underscore.js`
var _ = require("underscore");
file1 = require("./file1");
_.extend(module.exports, file1);
var something = {a: "a"};
// or do something else
_.extend(module.exports, {
obj: something
});
文件3.js
file2 = require("./file2.js");
console.log(file2.obj.a);