是否可以通过要求第二个模块将两个或多个模块合并为一个模块,如下所示:
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
}
}
模块.js
var xx = require('somemodule');
module.exports = function(x,y){
var1:'abcd';
var2:'efgh';
aa = function(x,y){
do something....
}
}
require('module1.js');
我试图想出一种拥有标准模块的方法,并在该模块中查找 custom.module,如果存在,则用自定义模块中的变量和对象覆盖标准模块中的任何现有变量和对象。
最佳答案
您的问题不太清楚您到底在寻找什么。但是,如果您想编写一个模块来公开另一个现有模块中的所有内容,您始终可以尝试导出相同的模块,并向其中添加所需的更多属性,前提是第一个模块导出一个对象:
var xx = require('module.js');
xx.something = function(x,y) {
...
}
module.exports = xx;
根据您的具体需求,使用 prototypal inheritance 也可能有意义。制作您的对象并将其导出。
关于node.js - Nodejs - 合并 2 个或更多模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177727/