node.js将变量暴露给模块? [英] node.js expose variable to module?

查看:202
本文介绍了node.js将变量暴露给模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于如何在node.js中创建模块的文章,你可以使用module.exports将模块内部公开给包含它的文件。真棒!

I have read a lot of articles about how to create modules in node.js, and you can use module.exports to expose module internals to the file which includes it.. awesome!

这是如何反过来的?我将使用以下作为示例: - )

How does this work the other way around? I'll use the following as an example :-)

 USER.JS

 function User() {
   this.property = 'value';
   this.doSomething = function() {
     var getStuff = mainFileFunction();
     // do something with getStuff
 }

 module.exports = User;







 MAIN.JS

 var myArray = [];
 myArray.push('something');
 myArray.push('something else');
 mainFileFunction() {
   for(thing in myArray) {
     return myArray[thing];
   }
 }

 var u = new user();

 log(u.property);  <---  THIS IS EXPOSED, COOL!

 u.doSomething();  <--- This will throw an error because mainFileFunction is not defined in user.js :-(

如果我要将mainFileFunction移动到用户文件,那么它仍然无法工作,因为myArray数组不会被定义...如果我也要移动它,我将无法使用它在main中的其他功能(我想要): - )

If I were to move mainFileFunction to the user file, then it still wouldn't work because the myArray array wouldn't be defined... and if I were to move that over too, I wouldn't be able to use it in other functions in main (which I want to) :-)

如果我在这里遗漏了一些非常明显的东西,我很抱歉...我想要的是从我包含的模块中公开我选择的部分(module.export适用于此)但我也希望公开从主文件到所有包含的所有内容..

I'm sorry if I'm missing something really obvious here... What I want is to expose the parts of my choosing from modules I include (module.export works for that) but I also want to expose everything from the main file to all the includes..

或者只是把一切都暴露在一切?那是完全混乱和可怕吗?

or just expose everything to everything? is that totally messy and horrible??

只是为了解释我在这里要做的事情...我想在不同的文件中定义类,但我想要将一堆它们实例化为主文件中的对象并将它们存储在数组中。我希望对象包含可以访问其他对象类型数组的方法。

Just to explain what I am trying to do here... I want to have classes defined in separate files, but I want to instantiate a bunch of them as objects in the main file and store them in arrays.. I want the objects to contain methods which can access arrays of the other object types.

多谢你们! : - )

Thanks guys! :-)

推荐答案

可以使用全局变量,或者具有正确的循环依赖关系(<$ c然而,这通常是一个坏习惯,可能导致可维护性问题。

You can use globals, or have a proper circular dependency (requireing both files), however - this is usually a bad habit which can lead to maintainability problems in the future.

取而代之的是$ c> require 两个文件,您可以使用依赖注入并将 doSomething 注入您的模块。
这基本上免费为您提供以下内容:

Instead, you can use dependency injection and inject doSomething into your module. This basically gives you the following for free:


  • 您可以测试用户稍后使用 doSomething 的简单模拟实现并验证代码的正确性

  • 用户的依赖关系显式而非隐式,这使得用户需要的东西显而易见。

  • You can test User with a simple mock implementation of doSomething later and verify the correctness of your code
  • The dependencies of a user are explicit and not implicit, which makes it obvious what a user needs.

我将提供两个实现,一个使用构造函数依赖注入和一个模块范围设置。

I'll provide two implementations, one using constructor dependency injection and one with a module wide setting.

 USER.JS

 function User(dependentFunction) {
   this.property = 'value';
   this.doSomething = function() {
     var getStuff = dependentFunction();
     // do something with getStuff
   }
 }

 module.exports = User;

MAIN.JS
...
var u = new User(mainFileFunction);
u.doSomething(); // this will now work, using mainFileFunction

这里发生的事情很简单,我们知道会发生什么on。

What happens here is fairly simple, and we know what's going on.

这也可以是模块范围设置

This can also be a module wide setting

USER.JS

function User(depFunc) {
  this.property = 'value';
  this.doSomething = function() {
    var getStuff = depFunc();
    // do something with getStuff
  }
}
function UserFactory(depFunc){ 
    return function(){
        return new User(depFunc);
    }
}
module.exports = UserFactory;

MAIN.JS

 var getUser = UserFactory(mainFileFunction);
 var u = getUser(); // will return a new user with the right function

这篇关于node.js将变量暴露给模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆