use*_*611 13 module duplication node.js
如果这是一个愚蠢的问题,我会道歉,但是如果我创建两个需要('http')的模块和我需要两个模块的主应用程序,或者需要模块又需要两个模块,同时还要求'http'用于自己的模块目的,我最终得到了http模块的三个实例,每个实例都锁定在不同的闭包范围内,或者节点是否重写了以避免这种情况?
换句话说,我最终得到的应用是:
// main app creates a closure containing a local instance of http, an instance of proxy1
// and an instance of proxy2, both of which are functions returned from closures that have instances of http in scope
var http = require('http'),
httpProxy1 = require('./proxy1'),
httpProxy2 = require('./proxy2');
/* ... do stuff with http, using proxy1 or proxy2 where appropriate ... */
// proxy1 creates a closure containing a local instance of http and exposes a single public method
var http = require('http');
module.exports = function (foo) { /* ... do stuff with http ... */ }
// proxy2 creates a closure containing a local instance of http and exposes a single public method
var http = require('http');
module.exports = function (foo) { /* ... do stuff with http that has nothing to do with the stuff proxy1 does ... */ }
Run Code Online (Sandbox Code Playgroud)
如果我也想独立使用proxy1,将它作为一个模块是有意义的,但即使是一个小项目,这也可能导致许多模块都需要重复核心模块,尤其是http和fs