我对Javascript中的内容并不了解。 我想要做的是理解python中dir()函数的等价物。我想看看目前范围内的可用内容。
我正在使用nodejs REPL console:
> 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 jail安全挑战。 我可以在这个挑战中看到这会返回{}
但是如果我从一个函数调用它,我可以看到真实的内容。 他们怎么能做到这一点?
由于
答案 0 :(得分:0)
您需要使用var(或let / const)声明变量。
用var声明它会将变量的范围限制在该函数内
function addition(a,b) { var local_variable=a+b; return local_variable; }
如果不使用var声明它,那么它将始终在全局上下文中自动声明,即使它在函数内。
如果您使用let and const更进一步,那么变量将具有块级范围。