我最近一直在使用nodejs,仍然在掌握模块系统,所以如果这是一个明显的问题,那么很抱歉。我希望代码大致如下:

A.js(主文件与node一起运行)

var ClassB = require("./b");

var ClassA = function() {
    this.thing = new ClassB();
    this.property = 5;
}

var a = new ClassA();

module.exports = a;

b.js

var a = require("./a");

var ClassB = function() {
}

ClassB.prototype.doSomethingLater() {
    util.log(a.property);
}

module.exports = ClassB;

我的问题似乎是我不能从ClassB的实例中访问ClassA的实例。

有什么正确的/更好的方法来构建模块来实现我想要的? 是否有更好的方法在模块间共享变量?


当前回答

有时引入第三个类(JohnnyHK建议)确实是人为的,所以除了Ianzz之外: 如果您确实需要更换模块。导出,例如,如果你正在创建一个类(如上面例子中的b.js文件),这也是可能的,只要确保在开始循环require的文件中,'模块。出口=…`语句发生在require语句之前。

A.js(主文件与node一起运行)

var ClassB = require("./b");

var ClassA = function() {
    this.thing = new ClassB();
    this.property = 5;
}

var a = new ClassA();

module.exports = a;

b.js

var ClassB = function() {
}

ClassB.prototype.doSomethingLater() {
    util.log(a.property);
}

module.exports = ClassB;

var a = require("./a"); // <------ this is the only necessary change

其他回答

虽然node.js确实允许循环的require依赖关系,但你已经发现它可能非常混乱,你最好重新构造你的代码以不需要它。也许可以创建第三个类,使用另外两个类来完成您所需要的。

现在不是2015年,大多数库(例如express)已经更新了更好的模式,因此不再需要循环依赖。我建议不要使用它们。

我知道我挖出了一个老答案…… 这里的问题是这个模块。exports定义在您需要ClassB之后。 (JohnnyHK的链接显示) 循环依赖关系在Node中工作得很好,它们是同步定义的。 如果使用得当,它们实际上解决了许多常见的节点问题(比如从其他文件访问express.js应用程序)

只需确保在需要具有循环依赖关系的文件之前定义了必要的导出。

这将打破:

var ClassA = function(){};
var ClassB = require('classB'); //will require ClassA, which has no exports yet

module.exports = ClassA;

这是可行的:

var ClassA = module.exports = function(){};
var ClassB = require('classB');

我一直使用这个模式来访问其他文件中的express.js应用程序:

var express = require('express');
var app = module.exports = express();
// load in other dependencies, which can now require this file and use app

博士TL;

只需使用导出。somember = somember而不是module。Exports ={//新对象}。

扩展的回答

在读完lanzz的回答后,我终于可以弄清楚这里发生了什么,所以我会就这个问题发表我的意见,扩展他的回答。

让我们来看看这个例子:

a.js

console.log("a starting");

console.log("a requires b");
const b = require("./b");
console.log("a gets b =", b);

function functionA() {
  console.log("function a");
}

console.log("a done");
exports.functionA = functionA;

b.js

console.log("b starting");

console.log("b requires a");
const a = require("./a");
console.log("b gets a =", a);

function functionB() {
  console.log("On b, a =", a)
}

console.log("b done");
exports.functionB = functionB;

main.js

const a = require("./a");
const b = require("./b");

b.functionB()

输出

a starting
a requires b
b starting
b requires a
b gets a = {}
b done
a gets b = { functionB: [Function: functionB] }
a done
On b, a = { functionA: [Function: functionA] }

在这里,我们可以看到b首先接收一个空对象作为a,然后一旦a完全加载,该引用将通过导出更新。functionA = functionA。如果将整个模块替换为另一个对象,则通过module。导出,那么b将失去来自a的引用,因为它将从开始指向相同的空对象,而不是指向新的对象。

如果你像这样导出:module。exports = {functionA: functionA},那么输出将是:

a starting
a requires b
b starting
b requires a
b gets a = {}
b done
a gets b = { functionB: [Function: functionB] }
a done
On b, a = {} // same empty object

解决方案是在需要任何其他控制器之前“向前声明”导出对象。所以如果你像这样构建你所有的模块,你就不会遇到这样的问题:

// Module exports forward declaration:
module.exports = {

};

// Controllers:
var other_module = require('./other_module');

// Functions:
var foo = function () {

};

// Module exports injects:
module.exports.foo = foo;

懒惰只在你需要的时候才要求呢?b。js是这样的

var ClassB = function() {
}
ClassB.prototype.doSomethingLater() {
    var a = require("./a");    //a.js has finished by now
    util.log(a.property);
}
module.exports = ClassB;

当然,将所有require语句放在文件的顶部是一个很好的实践。但在某些情况下,我原谅自己从一个不相关的模块中选择了一些内容。称其为hack,但有时这比引入进一步的依赖项,或添加额外的模块或添加新的结构(EventEmitter等)要好。