You can't quite do it the way you intend because node's architecture creates separate, individual global objects per module. However, you can get something similar using the mechanics of how this
works in javascript.
In plain js, given:
function doFunc () {
return this.foo;
}
var o = {foo:'bar'};
You can do:
o.dofunc = doFunc;
o.doFunc(); // returns 'bar'
Alternatively:
doFunc.call(o); // also returns 'bar'
So, applying this technique to modules:
In H.js:
module.exports.doFunc = function () {return this.foo}
In M.js:
var o = {
foo: 'bar',
dofunc : require('H.js').doFunc
};
o.dofunc();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…