我有一个 CommonJS 模块:
// main-module
module.exports = function () {
var foo,
someModule = require('other-module')(foo);
// A value is given to foo after other-module has been initialised
foo = "bar";
}
如您所见,这需要other-module:
// other-module.js
module.exports = function (foo) {
function example() {
console.log(foo);
// > "bar"
}
}
我希望example里面的函数other-module知道foo里面的变量main-module,即使它是在需要模块之后建立的。
other-module运行时,foo不会undefined。但是,关键是到我的example函数运行时,foo将被赋予bar.
上面的模式显然行不通。我需要实现什么设计模式?