List all global variables in Node.js

后端 未结 3 1232
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 22:25

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

相关标签:
3条回答
  • 2021-01-01 22:41

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

    0 讨论(0)
  • 2021-01-01 22:54

    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());
    }
    
    0 讨论(0)
  • 2021-01-01 23:02

    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

    0 讨论(0)
提交回复
热议问题