I'm developing a Node.js application that contains a game engine, and I basically have this pattern in my engine:
我正在开发一个包含游戏引擎的Node.js应用程序,我在我的引擎中基本上有这种模式:
A.js
var B = require('./B');
var A = module.exports = function () {
this.b = new B;
console.log(B.staticBar)
};
A.staticFoo = 'foo';
B.js
var A = require('./A');
var B = module.exports = function () {
console.log(A.staticFoo);
};
B.staticBar = 'bar';
So I want both A.staticFoo to be accessible in B.js and B.staticBar in A.js. Any idea how to do that?
所以我希望A.staticFoo可以在A.js中访问,而B.staticBar可以在A.js中访问。知道怎么做吗?
Thanks
EDIT : actually my static variables are config values, so another solution would be to group them into a config.js file and require that file in every other file, but I find it more elegant to define config variables directly as static members of related classes. Hope that's clear enough ;)
编辑:实际上我的静态变量是配置值,所以另一个解决方案是将它们分组到config.js文件中并在每个其他文件中需要该文件,但我发现将配置变量直接定义为相关类的静态成员更为优雅。希望足够清楚;)
2 个解决方案
#1
I would suggest separating your static state into a third module... By decoupling state from your module, you can operate either independently.
我建议将静态状态分成第三个模块......通过将状态与模块分离,您可以独立操作。
state.js
//state.js - changed by other modules...
module.exports = {
staticFoo: null,
staticBar: null
};
a.js
//a.js
var state = require('./state');
exports = module.exports = fnA;
...
function fnA() {
console.log(state.staticBar);
}
b.js
//b.js
var state = require('./state');
exports = module.exports = fnB;
...
function fnB() {
console.log(state.staticFoo);
}
Another example mentions something akin to dependency injection... given how modules work in JS, and that you can override for testing with proxyquire and the like, I tend to prefer the simpler requires structure over dealing with DI/IoC in JS as it muddles your project code.
另一个例子提到了类似于依赖注入的东西......考虑到模块在JS中是如何工作的,并且你可以使用proxyquire等来覆盖测试,我倾向于在JS中处理DI / IoC时更喜欢简单的需求结构你的项目代码。
I also like to do my requires, then my exports, then any methods within that module, usually just one method in a module.
我也喜欢做我的要求,然后我的导出,然后是该模块中的任何方法,通常只是模块中的一个方法。
#2
It would depend on the architecture of your code. BUT, working with other people's code is always different of course.
这取决于代码的体系结构。但是,与其他人的代码合作当然总是不同的。
The best choice is to separate your code into smaller module(s). If they're referencing each other it can challenging to build tests especially when the code grows.
最好的选择是将代码分成更小的模块。如果它们彼此引用,那么构建测试会很困难,特别是在代码增长时。
OR
If that's not possible you could always remove coupling through the use of references.
如果那是不可能的,你总是可以通过使用引用来删除耦合。
B.js
var _A;
exports.setA = function(ref) {
_A = ref;
};
var B = exports.B = function () {
console.log(_A.staticFoo);
};
And use B.setA(A)
to make sure B
has a reference to use A.staticFoo
并使用B.setA(A)确保B有一个使用A.staticFoo的引用
#1
I would suggest separating your static state into a third module... By decoupling state from your module, you can operate either independently.
我建议将静态状态分成第三个模块......通过将状态与模块分离,您可以独立操作。
state.js
//state.js - changed by other modules...
module.exports = {
staticFoo: null,
staticBar: null
};
a.js
//a.js
var state = require('./state');
exports = module.exports = fnA;
...
function fnA() {
console.log(state.staticBar);
}
b.js
//b.js
var state = require('./state');
exports = module.exports = fnB;
...
function fnB() {
console.log(state.staticFoo);
}
Another example mentions something akin to dependency injection... given how modules work in JS, and that you can override for testing with proxyquire and the like, I tend to prefer the simpler requires structure over dealing with DI/IoC in JS as it muddles your project code.
另一个例子提到了类似于依赖注入的东西......考虑到模块在JS中是如何工作的,并且你可以使用proxyquire等来覆盖测试,我倾向于在JS中处理DI / IoC时更喜欢简单的需求结构你的项目代码。
I also like to do my requires, then my exports, then any methods within that module, usually just one method in a module.
我也喜欢做我的要求,然后我的导出,然后是该模块中的任何方法,通常只是模块中的一个方法。
#2
It would depend on the architecture of your code. BUT, working with other people's code is always different of course.
这取决于代码的体系结构。但是,与其他人的代码合作当然总是不同的。
The best choice is to separate your code into smaller module(s). If they're referencing each other it can challenging to build tests especially when the code grows.
最好的选择是将代码分成更小的模块。如果它们彼此引用,那么构建测试会很困难,特别是在代码增长时。
OR
If that's not possible you could always remove coupling through the use of references.
如果那是不可能的,你总是可以通过使用引用来删除耦合。
B.js
var _A;
exports.setA = function(ref) {
_A = ref;
};
var B = exports.B = function () {
console.log(_A.staticFoo);
};
And use B.setA(A)
to make sure B
has a reference to use A.staticFoo
并使用B.setA(A)确保B有一个使用A.staticFoo的引用