私は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認識できるようにしたいと思います。foomain-module
other-module実行すると、 になりfooませんundefined。ただし、ポイントは、example関数が実行fooされるまでに、 の値が与えられるということですbar。
上記のパターンは明らかに機能しません。どの設計パターンを実装する必要がありますか?