Node.js global variables

前端 未结 6 1519
耶瑟儿~
耶瑟儿~ 2020-11-22 13:06

I asked here: Does Node.js require inheritance?

And I was told that I can set variables to the global scope by leaving out the variable.

This does not

6条回答
  •  -上瘾入骨i
    2020-11-22 13:45

    In Node.js, you can set global variables via the "global" or "GLOBAL" object:

    GLOBAL._ = require('underscore'); // But you "shouldn't" do this! (see note below)
    

    or more usefully...

    GLOBAL.window = GLOBAL;  // Like in the browser
    

    From the Node.js source, you can see that these are aliased to each other:

    node-v0.6.6/src/node.js:
    28:     global = this;
    128:    global.GLOBAL = global;
    

    In the code above, "this" is the global context. With the CommonJS module system (which Node.js uses), the "this" object inside of a module (i.e., "your code") is not the global context. For proof of this, see below where I spew the "this" object and then the giant "GLOBAL" object.

    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: {} }
    */

    ** Note: regarding setting "GLOBAL._", in general you should just do var _ = require('underscore');. Yes, you do that in every single file that uses Underscore.js, just like how in Java you do import com.foo.bar;. This makes it easier to figure out what your code is doing because the linkages between files are 'explicit'. It is mildly annoying, but a good thing. .... That's the preaching.

    There is an exception to every rule. I have had precisely exactly one instance where I needed to set "GLOBAL._". I was creating a system for defining "configuration" files which were basically JSON, but were "written in JavaScript" to allow a bit more flexibility. Such configuration files had no 'require' statements, but I wanted them to have access to Underscore.js (the entire system was predicated on Underscore.js and Underscore.js templates), so before evaluating the "configuration", I would set "GLOBAL._". So yeah, for every rule, there's an exception somewhere. But you had better have a darn good reason and not just "I get tired of typing 'require', so I want to break with the convention".

提交回复
热议问题