简体   繁体   中英

inspect nodejs global and local variables

I do not understand something in Javascript. What i want to do is to understand the equivalent of dir() function in python. I want to see what is available in my current scope.

I am playing with nodejs REPL console:

> myglobalvariable="toto"
'toto'

Now i want to see what is inside my current scope, so i type this in the console and here is what i get:

> 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' }

As you can see in the last line, i can see my global variable. Everything is fine for me. Displaying this is the equivalent of python dir function at this time.

But now, let's create a function which contains a local variable:

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

If i call my addition function, then type "this" in the console, here is what i see:

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

I do not understand why the local_variable is there. It should be a local variable for me...

I tried a javascript jail security challenge. I can see in this challenge that this returns {}

But if i call this from a function, i can see the real content. How can they make this possible ?

Thanks

You need to declare the variable with var (or let / const). Declaring it with var will constrain the scope of the variable to within that function function addition(a,b) { var local_variable=a+b; return local_variable; } function addition(a,b) { var local_variable=a+b; return local_variable; }

If you do not declare it with var, then it will always automatically be declared in the global context, even if it is within a function.

MDN var

If you go a step even further with let and const , then the variable will have a block level scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM