如何在 node.js 中请求文件并在请求方法中传递参数,而不是传递给模块?
我有一个必须加载的 module.js;为了工作需要objectX;
如何在node.js提供的require方法中将objectX传递给module.js?
谢谢,
// my module.js
objectX.add('abc');
// error: objectX is undefined
我想要一种方法来做到这一点,而不必更改我的所有课程,因为这会花费很多时间......而且他们的方式对于客户端来说具有良好的性能。 (我将客户端文件与服务器文件混合在一起***)
I have a module.js that must be loaded; In order to work needs objectX;
How do I pass the objectX to the module.js in the require method provided by node.js?
thanks
// my module.js
objectX.add('abc');
// error: objectX is undefined
I would like a way to do it, without having to change all my classes because would take a lot of time... and they way it is has good performance for the client side. (I mix clientfiles with serverfiles***)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您编写的模块可以导出单个函数。当您需要该模块时,请使用初始化参数调用该函数。该函数可以返回一个对象(哈希),您可以将其放入 require-ing 模块中的变量中。换句话说:
main.js
算术.js:
The module that you write can export a single function. When you require the module, call the function with your initialization argument. That function can return an Object (hash) which you place into your variable in the require-ing module. In other words:
main.js
arithmetic.js:
您可以通过链接“init”方法(随意命名)来避免更改实际导出的对象。
模块 TestModule.js:
然后像这样要求它:
You can avoid changing the actual exported object by chaining in an "init" method (name it whatever you want).
Module TestModule.js:
And then requiring it like this:
导出一些 init 方法并在请求后立即将 objectX 作为参数传递之类的解决方法怎么样?
What about workaround like export some init method and pass objectX as parameter right after requiring?