I'm trying to list all of the global variables, including those refering to built-in objects.
In Chrome's console I can simply type this
and get back all the keys, including things like String
, Number
, etc.
However when I do this in Node.js I get much less:
> Object.keys(this)
[ 'global',
'process',
'GLOBAL',
'root',
'Buffer',
'setTimeout',
'setInterval',
'clearTimeout',
'clearInterval',
'setImmediate',
'clearImmediate',
'console',
'module',
'require',
'_' ]
> this.eval
[Function: eval]
Where is this.eval
coming from?
Use the env command. The env command returns a list of all global variables that have been defined. If a global variable exists in a script that hasn't been run yet, it will not show up in the output from env .
What are Global Variables? Global variables are variables that can be declared with a value, and which can be accessed anywhere in a program. The scope of global variables is not limited to the function scope or any particular JavaScript file. It can be declared at one place and then used in multiple places.
To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.
It gives the current working directory of the Node. js process. __dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
The built-in properties of the global object are non-enumerable, so Object.keys
doesn't return them. You can use Object.getOwnPropertyNames
instead.
The following globals()
function will get you global namespace object:
function globals() { return this; }
With it you can list all variables of global namespace anytime you want:
function varsList() {
return Object.getOwnPropertyNames(globals());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With