如何在 node.js 中请求文件并在请求方法中传递参数,而不是传递给模块?

发布于 2024-12-26 10:14:30 字数 296 浏览 2 评论 0原文

我有一个必须加载的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2025-01-02 10:14:30

您编写的模块可以导出单个函数。当您需要该模块时,请使用初始化参数调用该函数。该函数可以返回一个对象(哈希),您可以将其放入 require-ing 模块中的变量中。换句话说:

main.js

var initValue = 0;
var a = require('./arithmetic')(initValue);
// It has functions
console.log(a);
// Call them
console.log(a.addOne());
console.log(a.subtractOne());

算术.js:

module.exports = function(initValue) {
  return {
    addOne: function() {
      return initValue + 1;
    },
    subtractOne: function() {
      return initValue - 1;
    },
  }
}

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

var initValue = 0;
var a = require('./arithmetic')(initValue);
// It has functions
console.log(a);
// Call them
console.log(a.addOne());
console.log(a.subtractOne());

arithmetic.js:

module.exports = function(initValue) {
  return {
    addOne: function() {
      return initValue + 1;
    },
    subtractOne: function() {
      return initValue - 1;
    },
  }
}
策马西风 2025-01-02 10:14:30

您可以通过链接“init”方法(随意命名)来避免更改实际导出的对象。

模块 TestModule.js:

var x = 0; // Some private module data

exports.init = function(nx) {
    x = nx; // Initialize the data
    return exports;
};

exports.sayHi = function() {
    console.log("HELLO THERE "+x);
};

然后像这样要求它:

var TM = require('./TestModule.js').init(20);
TM.sayHi();

You can avoid changing the actual exported object by chaining in an "init" method (name it whatever you want).

Module TestModule.js:

var x = 0; // Some private module data

exports.init = function(nx) {
    x = nx; // Initialize the data
    return exports;
};

exports.sayHi = function() {
    console.log("HELLO THERE "+x);
};

And then requiring it like this:

var TM = require('./TestModule.js').init(20);
TM.sayHi();
三生一梦 2025-01-02 10:14:30

导出一些 init 方法并在请求后立即将 objectX 作为参数传递之类的解决方法怎么样?

var module = require('moduleJS');
module.init(objectX)

What about workaround like export some init method and pass objectX as parameter right after requiring?

var module = require('moduleJS');
module.init(objectX)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文