如何在模块之间共享对象? [英] How can I share an object between my modules?
本文介绍了如何在模块之间共享对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个辅助模块H,我从主脚本M中require
:
So I have a helper module H, which I require
from my main script M:
M.js:
var o = {foo: 'bar'};
require('H.js').doFunc();
H.js:
module.exports = {doFunc: function(){ /* how can I set X to o.foo in M's scope? */ return X; }};
因此,人们可以朝这个方向揭露事物.我该怎么做才能以其他方式分享东西?
Thus one can expose things in this direction. What can I do to share things back the other way?
推荐答案
M.js
var o = {foo: 'bar'};
require('H.js')(o);
H.js
module.exports = function (o) {
console.log(o);
}
这篇关于如何在模块之间共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文