Javascript node.js 标准模块的全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4140661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variables for node.js standard modules?
提问by ajsie
I know that global variables are bad.
我知道全局变量不好。
But if I am using node's module "util" in 40 files in my framework, isn't it better to just declare it as a global variable like:
但是,如果我在框架的 40 个文件中使用节点的模块“util”,将其声明为全局变量不是更好,例如:
util = require('util');
in the index.js file instead of writing that line in 40 files?
在 index.js 文件中而不是在 40 个文件中写入该行?
Cause I often use the same 5-10 modules in each file, that would save a lot of time instead of copy paste all the time.
因为我经常在每个文件中使用相同的 5-10 个模块,这样可以节省大量时间,而不是一直复制粘贴。
Isn't DRY good in this case?
在这种情况下 DRY 不是很好吗?
采纳答案by Tor Valamo
Each module is supposed to be independent. The require doesn't cost anything anyways after the first one for each module.
每个模块都应该是独立的。在每个模块的第一个需求之后,无论如何都不需要任何费用。
What if you wanted to test one module alone? You'd be having a lot of issues because it wouldn't recognize some "global" requires that you have in your app.
如果您想单独测试一个模块怎么办?您会遇到很多问题,因为它无法识别您的应用程序中的某些“全局”要求。
Yes, globals are bad, even in this case. Globals almost always ruin: testability, encapsulation and ease of maintenance.
是的,即使在这种情况下,全局变量也很糟糕。全局变量几乎总是破坏:可测试性、封装性和易于维护。
Updated answer Jan. 2012
2012 年 1 月更新答案
The global
object is now a global inside each module. So every time you assign to a global variable (no scope) inside a module, that becomes part of the global
object of that module.
该global
对象现在是每个模块内的全局对象。因此,每次分配给模块内的全局变量(无作用域)时,它都会成为global
该模块对象的一部分。
The global
object is therefore still not global, and cannot be used as such.
global
因此该对象仍然不是global,并且不能这样使用。
Updated Dec. 2012
2012 年 12 月更新
The global
object now has the global scope within the application and can be used to store any data/functions that need to be accessed from all modules.
该global
对象现在在应用程序中具有全局作用域,可用于存储需要从所有模块访问的任何数据/函数。
回答by Robin Duckett
You could just have a common module.
你可以只拥有一个通用模块。
common.js:
常见的.js:
Common = {
util: require('util'),
fs: require('fs'),
path: require('path')
};
module.exports = Common;
app.js:
应用程序.js:
var Common = require('./common.js');
console.log(Common.util.inspect(Common));
回答by summatix
global.util = require('util');
There's a section about global objects in the node documentation.
节点文档中有一个关于全局对象的部分。
However, globals should be used with care. By adding modules to the global space you reduce testability and encapsulation. But there are cases where using this method is acceptable. For example, I add functions and objects to the global namespace to use within my unit test scripts.
但是,应谨慎使用全局变量。通过向全局空间添加模块,您会降低可测试性和封装性。但在某些情况下,使用这种方法是可以接受的。例如,我将函数和对象添加到全局命名空间以在我的单元测试脚本中使用。
回答by Dustin Graham
I'm confused by the answers in this thread.
我对这个线程中的答案感到困惑。
I am able to do this...
我能够做到这一点...
File: test.js
文件:test.js
global.mytest = {
x: 3,
y: function() { console.log('Works.'); }
};
File: test2.js
文件:test2.js
console.log('Does this work?');
mytest.y();
File: server.js
文件:server.js
require('test.js');
require('test2.js');
And it seems to work as the question needed. The first require places the mytest object into the global scope, then the second require can access that object without any other qualifiers.
它似乎可以满足所需的问题。第一个 require 将 mytest 对象放入全局范围,然后第二个 require 可以访问该对象而无需任何其他限定符。
I was trying to figure this out (which brought me to this thread from a Google search) and I wanted to post what seems to work for me now. Maybe things have changed since the original answers.
我试图弄清楚这一点(这让我从谷歌搜索到了这个线程),我想发布现在似乎对我有用的东西。也许自最初的答案以来事情已经发生了变化。
回答by tedeh
I have successfully been using the process
object for passing around my configuration object. While in theory suffering from the exact same issues as mentioned above (encapsulation, testability and so forth) it works fine when using only non-state modifying properties (a hash table with primitives, basically).
我已经成功地使用该process
对象来传递我的配置对象。虽然理论上存在与上述完全相同的问题(封装、可测试性等),但它在仅使用非状态修改属性(基本上是带有原语的哈希表)时工作正常。
回答by tedeh
If you wrap your modules in blocks (e.g. anon functions) you can bind to a local name (via parameter or 'var') and then have any arbitrary long (perhaps "package" labeled) name you want (if you even need a global at this point).
如果您将模块包装在块中(例如匿名函数),您可以绑定到本地名称(通过参数或“var”),然后使用您想要的任意长(可能标记为“包”)名称(如果您甚至需要一个全局名称)这一点)。
For instance, my modules often look similar to:
例如,我的模块通常类似于:
;(function ($, $exp, other) {
$(...)
other.xyz()
$exp.MyExportedObject = ...;
})(jQuery, window, some_module.other_expression) // end module
I use jQuery with noConflict, this the former, and the latter show you can do this for any expression -- global, require, computed, in-line, whatever... this same "wrapping" approach can be used to eliminate all (or almost all) "special named" globals -- globals must exist at some level, however, removing potentially conflicts is a very big win.
我将 jQuery 与 noConflict 一起使用,这是前者,后者表明您可以对任何表达式执行此操作——全局、需要、计算、内联等等……这种相同的“包装”方法可用于消除所有 (或几乎所有)“特殊命名”全局变量——全局变量必须存在于某个级别,但是,消除潜在的冲突是一个非常大的胜利。