从所需模块构造新对象的模式或最佳方法? [英] Pattern or best way to construct new objects from required module?

查看:53
本文介绍了从所需模块构造新对象的模式或最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将我的代码分解为许多Typescript类和接口,尽管它可以很好地工作并且非常适合测试和维护,但我想知道是否有更好的方法来构造所需的对象.据我所知,我的选择是要求和构造如下:

I've broken my code up into a number of Typescript classes and interfaces, and while it works well and is great for testing and maintenance, I'm wondering if there's a better way to construct required objects. As far as I know, my choices are to require and construct like so:

const MyModule = require('./src/mymodule');
const myModule = new MyModule();

const myModule = new (require('./src/mymodule'))();

是否还有其他解决方案或模式可以使其更具可读性/简洁性?

Is there another solution or pattern as an alternative that might make this more readable/clean?

推荐答案

如果需要在给定的模块中创建多个对象,则可以使用第一个方案,在该方案中,首先将模块句柄保存到局部变量中,这样您就可以多次引用:

If you need to create multiple objects within a given module, then you would use your first scheme where you save the module handle to a local variable first so you can reference it multiple times:

const SomeConstructor = require('./src/mymodule');

const myObj1 = new SomeConstructor();
const myObj2 = new SomeConstructor();

如果您只需要在给定的模块中创建一个该类型的对象,并且在该模块中不需要该模块的其他导出,则如您所显示的,您不需要存储构造函数/模块首先处理它自己的变量,这样就可以直接使用它,如所示:

If you only need to create one object of that type within a given module and there are no other exports from that module that you need in this module, then as you have shown you don't need to store the constructor/module handle in its own variable first so you can just use it directly as you've shown:

const myObj = new (require('./src/mymodule'))();

由于此语法看起来有些尴尬,通常导出一个工厂函数自动为您应用new .例如,http模块公开了一个工厂函数来创建服务器:

Because this syntax looks slightly awkward, it is common to export a factory function that automatically applies new for you. For example, the http module exposes a factory function to create a server:

const http = require('http');
const server = http.createServer(...);

在您的示例中,您可以执行以下任一操作:

// module constructor is a factory function for creating new objects
const myObj = require('./src/mymodule')();

// module exports a factory function for creating new objects
const myObj = require('./src/mymodule').createObj();


Express服务器框架中的一个示例是:


An example in the Express server framework is this:

const app = require('express')();

其中,如果要从express模块​​访问其他导出,则:

Of, if you want to access other exports from the express module:

const express = require('express');
const app = express();

app.use(express.static('public'));

在Express示例中,它同时导出工厂功能和作为工厂功能属性的其他方法.您可以选择仅使用工厂功能(上面的第一个示例),也可以保存工厂功能,以便还可以访问其上的某些属性(第二个示例).

In the Express example, it exports both a factory function and other methods that are properties on the factory function. You can choose to use only the factory function (first example above) or you can save the factory function so you can also access some of the properties on it (second example).

这篇关于从所需模块构造新对象的模式或最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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