节点.js全局变量
我在这里问:Node.js是否需要继承?
我被告知,我可以通过省略变量来将变量设置为全局范围。
这对我不起作用。
也就是说,以下内容不会使所需文件可用。_
_ = require('underscore');
我可以设置Express.js's,并在其他地方提供。app.set
这是应该如何工作的吗?
我在这里问:Node.js是否需要继承?
我被告知,我可以通过省略变量来将变量设置为全局范围。
这对我不起作用。
也就是说,以下内容不会使所需文件可用。_
_ = require('underscore');
我可以设置Express.js's,并在其他地方提供。app.set
这是应该如何工作的吗?
在 Node.js 中,您可以通过“global”或“GLOBAL”对象设置全局变量:
GLOBAL._ = require('underscore'); // But you "shouldn't" do this! (see note below)
或者更有用的...
GLOBAL.window = GLOBAL; // Like in the browser
从 Node.js源中,您可以看到它们彼此别名:
node-v0.6.6/src/node.js:
28: global = this;
128: global.GLOBAL = global;
在上面的代码中,“this”是全局上下文。对于CommonJS模块系统(Node.js使用),模块内部的“this”对象(即“你的代码”)不是全局上下文。为了证明这一点,请参阅下面我喷出“this”对象,然后是巨大的“GLOBAL”对象。
console.log("\nTHIS:");
console.log(this);
console.log("\nGLOBAL:");
console.log(global);
/* Outputs ...
THIS:
{}
GLOBAL:
{ ArrayBuffer: [Function: ArrayBuffer],
Int8Array: { [Function] BYTES_PER_ELEMENT: 1 },
Uint8Array: { [Function] BYTES_PER_ELEMENT: 1 },
Int16Array: { [Function] BYTES_PER_ELEMENT: 2 },
Uint16Array: { [Function] BYTES_PER_ELEMENT: 2 },
Int32Array: { [Function] BYTES_PER_ELEMENT: 4 },
Uint32Array: { [Function] BYTES_PER_ELEMENT: 4 },
Float32Array: { [Function] BYTES_PER_ELEMENT: 4 },
Float64Array: { [Function] BYTES_PER_ELEMENT: 8 },
DataView: [Function: DataView],
global: [Circular],
process:
{ EventEmitter: [Function: EventEmitter],
title: 'node',
assert: [Function],
version: 'v0.6.5',
_tickCallback: [Function],
moduleLoadList:
[ 'Binding evals',
'Binding natives',
'NativeModule events',
'NativeModule buffer',
'Binding buffer',
'NativeModule assert',
'NativeModule util',
'NativeModule path',
'NativeModule module',
'NativeModule fs',
'Binding fs',
'Binding constants',
'NativeModule stream',
'NativeModule console',
'Binding tty_wrap',
'NativeModule tty',
'NativeModule net',
'NativeModule timers',
'Binding timer_wrap',
'NativeModule _linklist' ],
versions:
{ node: '0.6.5',
v8: '3.6.6.11',
ares: '1.7.5-DEV',
uv: '0.6',
openssl: '0.9.8n' },
nextTick: [Function],
stdout: [Getter],
arch: 'x64',
stderr: [Getter],
platform: 'darwin',
argv: [ 'node', '/workspace/zd/zgap/darwin-js/index.js' ],
stdin: [Getter],
env:
{ TERM_PROGRAM: 'iTerm.app',
'COM_GOOGLE_CHROME_FRAMEWORK_SERVICE_PROCESS/USERS/DDOPSON/LIBRARY/APPLICATION_SUPPORT/GOOGLE/CHROME_SOCKET': '/tmp/launch-nNl1vo/ServiceProcessSocket',
TERM: 'xterm',
SHELL: '/bin/bash',
TMPDIR: '/var/folders/2h/2hQmtmXlFT4yVGtr5DBpdl9LAiQ/-Tmp-/',
Apple_PubSub_Socket_Render: '/tmp/launch-9Ga0PT/Render',
USER: 'ddopson',
COMMAND_MODE: 'unix2003',
SSH_AUTH_SOCK: '/tmp/launch-sD905b/Listeners',
__CF_USER_TEXT_ENCODING: '0x12D732E7:0:0',
PATH: '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/bin:/usr/X11/bin',
PWD: '/workspace/zd/zgap/darwin-js',
LANG: 'en_US.UTF-8',
ITERM_PROFILE: 'Default',
SHLVL: '1',
COLORFGBG: '7;0',
HOME: '/Users/ddopson',
ITERM_SESSION_ID: 'w0t0p0',
LOGNAME: 'ddopson',
DISPLAY: '/tmp/launch-l9RQXI/org.x:0',
OLDPWD: '/workspace/zd/zgap/darwin-js/external',
_: './index.js' },
openStdin: [Function],
exit: [Function],
pid: 10321,
features:
{ debug: false,
uv: true,
ipv6: true,
tls_npn: false,
tls_sni: true,
tls: true },
kill: [Function],
execPath: '/usr/local/bin/node',
addListener: [Function],
_needTickCallback: [Function],
on: [Function],
removeListener: [Function],
reallyExit: [Function],
chdir: [Function],
debug: [Function],
error: [Function],
cwd: [Function],
watchFile: [Function],
umask: [Function],
getuid: [Function],
unwatchFile: [Function],
mixin: [Function],
setuid: [Function],
setgid: [Function],
createChildProcess: [Function],
getgid: [Function],
inherits: [Function],
_kill: [Function],
_byteLength: [Function],
mainModule:
{ id: '.',
exports: {},
parent: null,
filename: '/workspace/zd/zgap/darwin-js/index.js',
loaded: false,
exited: false,
children: [],
paths: [Object] },
_debugProcess: [Function],
dlopen: [Function],
uptime: [Function],
memoryUsage: [Function],
uvCounters: [Function],
binding: [Function] },
GLOBAL: [Circular],
root: [Circular],
Buffer:
{ [Function: Buffer]
poolSize: 8192,
isBuffer: [Function: isBuffer],
byteLength: [Function],
_charsWritten: 8 },
setTimeout: [Function],
setInterval: [Function],
clearTimeout: [Function],
clearInterval: [Function],
console: [Getter],
window: [Circular],
navigator: {} }
*/
**注意:关于设置“GLOBAL._”,一般来说,您应该只做.是的,您可以在每个使用Underscore.js的文件中执行此操作,就像在Java中所做的那样。这样可以更轻松地确定代码正在执行的操作,因为文件之间的链接是“显式的”。这有点烦人,但这是一件好事。....这就是讲道。var _ = require('underscore');
import com.foo.bar;
每条规则都有一个例外。我正好有一个实例需要设置“GLOBAL._”。我正在创建一个系统来定义“配置”文件,这些文件基本上是JSON,但是“用JavaScript编写”,以允许更大的灵活性。这样的配置文件没有“require”语句,但我希望它们能够访问Underscore.js(整个系统都基于Underscore.js和Underscore.js模板),所以在评估“配置”之前,我会设置“GLOBAL._”。所以,是的,对于每条规则,在某个地方都有一个例外。但你最好有一个很好的理由,而不仅仅是“我厌倦了输入'要求',所以我想打破惯例”。