如何正确模块化node.js项目? [英] How to correctly modularize a node.js project?

查看:102
本文介绍了如何正确模块化node.js项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照类构造函数模式创建一个node.js项目:

I'm creating a node.js project following the class constructor pattern:

function my_class(x,y){
    this.x = x;
    this.y = y;
}

项目的起点是main.js文件.项目的任何类都必须能够访问在main.js上定义的全局对象(例如"world"和"socket").我发现了4种选择:

The starting point of the project is the main.js file. Any class of the project must be able to access global objects (such as "world" and "socket") which are defined on main.js. I found 4 options:

  1. 我在main.js中定义了我的班级.他们将可以访问main.js的全局变量,但main.js会变得肿.

  1. I define my classes inside main.js. They'll have access to main.js's globals for being on it's closure, but main.js will become bloated.

我将类移至另一个文件,例如my_class.jsrequire().这不起作用,因为my_class的实例将丢失关闭上下文,并且不再能够访问main.js的全局变量.

I move the class to another file such as my_class.js and require() it. This doesn't work because my_class's instances will lose the closure context and no longer be able to access main.js's globals.

我将类移动到另一个文件,并将依赖项手动注入到其构造函数中(例如:my_class(world,socket)).问题在于,代码变得更加复杂,并且诸如"my_instance.world"之类的怪异语义在源头上弹出,这是无稽之谈,因为"world"不是my_class的属性.

I move the class to another file and manually inject dependencies to it's constructor (ex: my_class(world,socket)). The problem is, code becomes much more complicated and weird semantics such as "my_instance.world" pop on the source, which is nonsense because "world" is not property of my_class.

我将类移动到另一个文件,并要求使用my_class = eval(fs.readFileSync(())而不是require.当my_class获取main.js的闭包上下文时,此方法就很好了,这是我正在使用的解决方案,但似乎很笨拙.

I move the class to another file and require it using my_class = eval(fs.readFileSync(()) instead of require. This works just fine as my_class gets main.js's closure context, and is the solution I'm using, but seems hacky.

哪个是正确的模块化此类node.js项目的正确方法?

Which is the right way to modularize such node.js project?

推荐答案

您的问题似乎很棘手,因为您具有循环依赖项:main.js取决于my_classmy_class的功能取决于main.js的数据.

Your problem seems tricky because you have a circular dependency: main.js depends on the functionality of my_class and my_class depends on the data of main.js.

通过将main.js的数据放入global对象中,可以解决循环依赖性:

By putting the data of main.js into the global object you resolve the circular dependency:

  • main.js取决于my_class.js
  • 的功能
  • main.js取决于global对象中的数据
  • my_class.js取决于global对象中的数据
  • main.js depends on the functionality of my_class.js
  • main.js depends on the data in the global object
  • my_class.js depends on the data in the global object

现在,要摆脱将数据放入global对象的问题,请使用让我们说data.js的方式实现第三个模块.然后,您需要以下来源:

Now, to get rid of putting the data into the global object, implement a third module in let us say data.js. Then you require the sources like this:

  • main.js需要data.js
  • main.js需要my_class.js
  • my_class.js需要data.js
  • main.js requires data.js
  • main.js requires my_class.js
  • my_class.js requires data.js

由于node.js中的模块是单例,因此main.jsmy_class.js都将获得data.js的相同实例.

Since modules in node.js are singletons both main.js and my_class.js will get the same instance of data.js.

这篇关于如何正确模块化node.js项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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