nodejs - 合并2个或更多模块

时间:2020-12-30 22:59:18

Is it possible to merge 2 or more modules into a single module by requiring the second module something like this:

是否可以通过要求第二个模块将两个或更多模块合并到一个模块中:

module1.js

var xx = require('somemodule');
var yy = require('anothermodule');
module.exports = function(x,y){
    var1:'defg';
    var2:'efgh';
    aa = function(x,y){
        do something different
    }
}

module.js

var xx = require('somemodule');
module.exports = function(x,y){
    var1:'abcd';
    var2:'efgh';
    aa = function(x,y){
        do something....
    }
}
require('module1.js');

I am trying to come up with a way of having a standard module and in that module look for a custom.module, if it exists then override any existing vars and objects in the standard module with those from the custom module.

我试图想出一种标准模块的方法,并在该模块中查找custom.module,如果它存在,则覆盖标准模块中的任何现有变量和对象与自定义模块中的变量。

1 个解决方案

#1


2  

Your question is not very clear about what you exactly you are looking for. But if you want to write a module to expose everything from another existing module, you can always try exporting the same module, and adding more attributes which you need into it, provided that the first module exports an object :

您的问题并不十分清楚,您正在寻找什么。但是如果你想编写一个模块来公开来自另一个现有模块的所有模块,你总是可以尝试导出相同的模块,并添加你需要的更多属性,只要第一个模块导出一个对象:

var xx = require('module.js');
xx.something = function(x,y) {
    ...
}
module.exports = xx;

Depending on what exactly you want, it might make also sense to use prototypal inheritance to make your object and export it.

根据您的具体需要,使用原型继承来制作对象并将其导出也是有意义的。

#1


2  

Your question is not very clear about what you exactly you are looking for. But if you want to write a module to expose everything from another existing module, you can always try exporting the same module, and adding more attributes which you need into it, provided that the first module exports an object :

您的问题并不十分清楚,您正在寻找什么。但是如果你想编写一个模块来公开来自另一个现有模块的所有模块,你总是可以尝试导出相同的模块,并添加你需要的更多属性,只要第一个模块导出一个对象:

var xx = require('module.js');
xx.something = function(x,y) {
    ...
}
module.exports = xx;

Depending on what exactly you want, it might make also sense to use prototypal inheritance to make your object and export it.

根据您的具体需要,使用原型继承来制作对象并将其导出也是有意义的。