20

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?

ldmat
  • 935
  • 1
  • 6
  • 15

3 Answers3

12

The built-in properties of the global object are non-enumerable, so Object.keys doesn't return them. You can use Object.getOwnPropertyNames instead.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • For anyone working with some in-browser coding tools, like codepen, [tslang](https://www.typescriptlang.org/play), etc, be aware this answer will not work, and you need to test with a local tool like `ts-node`, or in the browser console directly – New Alexandria Dec 28 '20 at 19:47
  • @NewAlexandria [Works fine for me](https://www.typescriptlang.org/play#code/MYewdgziA2CmB00QHMAUB5ARgK1sALvMrPugO5gAKATiAA6zX4CeAcgIYC2sEqEs0AGYBKYQG4gA) – Bergi Dec 28 '20 at 21:21
  • It does not work, I get `TypeError: Cannot convert undefined or null to object` – user5359531 Mar 01 '22 at 20:11
  • @user5359531 What code did you run and where? Apparently you did not pass the global object. – Bergi Mar 01 '22 at 20:19
  • I ran `Object.getOwnPropertyNames(this)` – user5359531 Mar 01 '22 at 20:31
  • @user5359531 And *where*? The value of `this` depends on a lot of things, and apparently your `this` is not the global object. Use something else. – Bergi Mar 01 '22 at 20:47
  • looks like its running [here](https://github.com/common-workflow-language/cwltool/blob/main/cwltool/sandboxjs.py#L146); `node --eval ...` – user5359531 Mar 03 '22 at 15:10
  • @user5359531 `node --eval 'console.log(Object.getOwnPropertyNames(this))'` works just fine for me – Bergi Mar 04 '22 at 03:38
10

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());
}

UPDATE:

In modern browsers you can use globalThis namespace object that holds variables of global namespace:

function globalVarsList() {
  return Object.getOwnPropertyNames(globalThis);
}

for(const name of globalVarsList()) {
   console.log(name); 
}

This will work in strict mode too.

c-smile
  • 26,734
  • 7
  • 59
  • 86
4

You can use the Object.getOwnPropertyNames(this) .As without passing the "this" as argument or parameter referring to Object owner's properties, the getOwnPropertyNames() function won't return anything.

Answering your question as to where the eval comes from check this link out. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

N Djel Okoye
  • 950
  • 12
  • 10