在模块中注入全局变量

时间:2013-11-12 08:16:01

标签: node.js

在nodejs中,有没有办法可以做到这一点?

//require something here and inject in this module
//all the properties visible in that module ex:

require("color"); 
//inject directly making properties from 'color' module global to this module.

console.log(red);
//'red' variable here came from 'color' module

我不知道这是否已经正确或我遗失了什么。

1 个答案:

答案 0 :(得分:0)

我认为你必须明确设置你想要“导入”的每个值:


var color = require("color");

var red = color.red,
    green = color.green;

// …

此外,您可以使用with,但广泛不推荐: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with