簡體   English   中英

檢查nodejs的全局和局部變量

[英]inspect nodejs global and local variables

我不懂Javascript。 我想做的是了解python中dir()函數的等效功能。 我想看看我當前的范圍內有什么可用。

我正在使用nodejs REPL控制台:

> myglobalvariable="toto"
'toto'

現在,我想查看當前范圍內的內容,因此我在控制台中輸入了以下內容:

> this
{ global: [Circular],
  process: 
   process {
     title: 'nodejs',
     version: 'v4.8.2',
     moduleLoadList: 
      [ 'Binding contextify',
        'Binding natives',
        'NativeModule events',
        'NativeModule buffer',
        'Binding buffer',
        'NativeModule internal/util',
        'Binding util',
        'NativeModule timers',
        'Binding timer_wrap',
        'NativeModule _linklist',
        'NativeModule assert',
        'NativeModule util',
        'Binding uv',
        'NativeModule tty',
        'NativeModule net',
        'NativeModule stream',
        'NativeModule _stream_readable',
        'NativeModule _stream_writable',
        'NativeModule _stream_duplex',
        'NativeModule _stream_transform',
        'NativeModule _stream_passthrough',
        'NativeModule internal/net',
        'Binding cares_wrap',
        'Binding tty_wrap',
        'Binding tcp_wrap',
        'Binding pipe_wrap',
        'Binding stream_wrap',
        'NativeModule internal/repl',
        'NativeModule readline',
        'NativeModule repl',
        'NativeModule internal/module',
        'NativeModule vm',
        'NativeModule path',
        'NativeModule fs',
        'Binding fs',
        'NativeModule constants',
        'Binding constants',
        'Binding fs_event_wrap',
        'NativeModule console',
        'Binding signal_wrap',
        'NativeModule module',
        'NativeModule domain',
        'NativeModule os',
        'Binding os',
        'NativeModule string_decoder' ],
     versions: 
      { http_parser: '2.7.0',
        node: '4.8.2',
        v8: '4.5.103.46',
        uv: '1.9.1',
        zlib: '1.2.8',
        ares: '1.10.1-DEV',
        icu: '57.1',
        modules: '46',
        openssl: '1.0.2k' },
     arch: 'x64',
     platform: 'linux',
     release: 
      { name: 'node',
        lts: 'Argon',
        sourceUrl: 'https://nodejs.org/download/release/v4.8.2/node-v4.8.2.tar.gz',
        headersUrl: 'https://nodejs.org/download/release/v4.8.2/node-v4.8.2-headers.tar.gz' },
     argv: [ '/usr/bin/nodejs' ],
     execArgv: [],
     env: 
      { LS_COLORS: 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:',
        SSH_CONNECTION: '192.168.56.1 52776 192.168.56.102 22',
        LANG: 'fr_FR.UTF-8',
        _VIRTUALENVWRAPPER_API: ' mkvirtualenv rmvirtualenv lsvirtualenv showvirtualenv workon add2virtualenv cdsitepackages cdvirtualenv lssitepackages toggleglobalsitepackages cpvirtualenv setvirtualenvproject mkproject cdproject mktmpenv mkvirtualenv rmvirtualenv lsvirtualenv showvirtualenv workon add2virtualenv cdsitepackages cdvirtualenv lssitepackages toggleglobalsitepackages cpvirtualenv setvirtualenvproject mkproject cdproject mktmpenv',
        XDG_SESSION_ID: '2',
        USER: 'me',
        PWD: '/home/me',
        HOME: '/home/me',
        SSH_CLIENT: '192.168.56.1 52776 22',
        SSH_TTY: '/dev/pts/0',
        MAIL: '/var/mail/me',
        VIRTUALENVWRAPPER_SCRIPT: '/usr/share/virtualenvwrapper/virtualenvwrapper.sh',
        TERM: 'xterm-256color',
        SHELL: '/bin/bash',
        SHLVL: '1',
        LOGNAME: 'me',
        DBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus',
        XDG_RUNTIME_DIR: '/run/user/1000',
        PATH: '/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games',
        _: '/usr/bin/nodejs' },
     pid: 1042,
     features: 
      { debug: false,
        uv: true,
        ipv6: true,
        tls_npn: true,
        tls_alpn: true,
        tls_sni: true,
        tls_ocsp: true,
        tls: true },
     _needImmediateCallback: false,
     execPath: '/usr/bin/nodejs',
     debugPort: 5858,
     _startProfilerIdleNotifier: [Function: _startProfilerIdleNotifier],
     _stopProfilerIdleNotifier: [Function: _stopProfilerIdleNotifier],
     _getActiveRequests: [Function: _getActiveRequests],
     _getActiveHandles: [Function: _getActiveHandles],
     reallyExit: [Function: reallyExit],
     abort: [Function: abort],
     chdir: [Function: chdir],
     cwd: [Function: cwd],
     umask: [Function: umask],
     getuid: [Function: getuid],
     geteuid: [Function: geteuid],
     setuid: [Function: setuid],
     seteuid: [Function: seteuid],
     setgid: [Function: setgid],
     setegid: [Function: setegid],
     getgid: [Function: getgid],
     getegid: [Function: getegid],
     getgroups: [Function: getgroups],
     setgroups: [Function: setgroups],
     initgroups: [Function: initgroups],
     _kill: [Function: _kill],
     _debugProcess: [Function: _debugProcess],
     _debugPause: [Function: _debugPause],
     _debugEnd: [Function: _debugEnd],
     hrtime: [Function: hrtime],
     cpuUsage: [Function: cpuUsage],
     dlopen: [Function: dlopen],
     uptime: [Function: uptime],
     memoryUsage: [Function: memoryUsage],
     binding: [Function: binding],
     _linkedBinding: [Function: _linkedBinding],
     _events: 
      { newListener: [Function],
        removeListener: [Function],
        SIGWINCH: [Object] },
     _rawDebug: [Function],
     _eventsCount: 3,
     domain: [Getter/Setter],
     _maxListeners: undefined,
     EventEmitter: 
      { [Function: EventEmitter]
        EventEmitter: [Circular],
        usingDomains: true,
        defaultMaxListeners: 10,
        init: [Function],
        listenerCount: [Function] },
     _fatalException: [Function],
     _exiting: false,
     assert: [Function],
     config: { target_defaults: [Object], variables: [Object] },
     nextTick: [Function: nextTick],
     _tickCallback: [Function: _tickDomainCallback],
     _tickDomainCallback: [Function: _tickDomainCallback],
     stdout: [Getter],
     stderr: [Getter],
     stdin: [Getter],
     openStdin: [Function],
     exit: [Function],
     kill: [Function] },
  GLOBAL: [Circular],
  root: [Circular],
  Buffer: 
   { [Function: Buffer]
     poolSize: 8192,
     from: [Function],
     alloc: [Function],
     allocUnsafe: [Function],
     allocUnsafeSlow: [Function],
     isBuffer: [Function: isBuffer],
     compare: [Function: compare],
     isEncoding: [Function],
     concat: [Function],
     byteLength: [Function: byteLength] },
  clearImmediate: [Function],
  clearInterval: [Function],
  clearTimeout: [Function],
  setImmediate: [Function],
  setInterval: [Function],
  setTimeout: [Function],
  console: [Getter],
  module: 
   Module {
     id: '<repl>',
     exports: {},
     parent: undefined,
     filename: null,
     loaded: false,
     children: [],
     paths: 
      [ '/home/me/repl/node_modules',
        '/home/me/node_modules',
        '/home/node_modules',
        '/node_modules',
        '/home/me/.node_modules',
        '/home/me/.node_libraries',
        '/usr/lib/nodejs' ] },
  require: 
   { [Function: require]
     resolve: [Function],
     main: undefined,
     extensions: { '.js': [Function], '.json': [Function], '.node': [Function] },
     cache: {} },
  _: [Circular],
  myglobalvariable: 'toto' }

正如您在最后一行看到的那樣,我可以看到我的全局變量。 一切對我都很好。 顯示此內容相當於此時的python dir函數。

現在,讓我們創建一個包含局部變量的函數:

> function addition(a,b) { local_variable=a+b; return local_variable; }

如果我調用加法函數,則在控制台中鍵入“ this”,這是我看到的內容:

  > this
  ...
  myglobalvariable: 'toto',
  addition: [Function: addition],
  local_variable: 9 }

我不明白為什么local_variable在那。 對我來說應該是局部變量...

我嘗試了JavaScript監獄安全挑戰。 我可以在此挑戰中看到返回{}

但是,如果我從函數中調用它,則可以看到真正的內容。 他們如何使這成為可能?

謝謝

您需要使用var聲明變量(或let / const)。 用var聲明它會將該變量的范圍限制在該函數的function addition(a,b) { var local_variable=a+b; return local_variable; } function addition(a,b) { var local_variable=a+b; return local_variable; }

如果不使用var聲明它,那么即使它在函數中,也總是會在全局上下文中自動聲明它。

MDN變量

如果您進一步使用let和const ,則該變量將具有塊級作用域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM