Python's 'from ... import *' in Node.js
Is there anything equivarent to Python's 'from foobar import *' in Node.js?
Now I wrote the following code:
var foobar = require('foobar'),
func1 = foobar.func1,
gvar2 = foobar.gvar2,
const3 = foobar.const3;
I think this is ugly, because a lot of names appeared twice.
Python provides smart solution which removes duplications:
from foobar import func1, gvar2, const开发者_如何学JAVA3
Does Node.js provide similar way?
No, it does not; at least, I am not aware of any way to do this easily.
Node uses the CommonJS module system, which requires a function, require
, that returns an exported API for a module.
function mixin(mod,scope)
{
if (!scope)
scope=global;
var module = require(mod);
for (key in module)
scope[key] = module[key];
}
mixin('http');
var s = createServer();
精彩评论