[removed] enumerate global vars

前端 未结 3 893
南旧
南旧 2021-01-27 05:52
a = 123; // becomes global var

var b = 234; // local var

function f() {} // local func

g = function() {} // got a global func

now how to print

相关标签:
3条回答
  • 2021-01-27 06:23

    All global variables are stored on the window object in a browser. Alternatively, they are stored on the global or GLOBAL object in Node.js. You can enumerate that object's keys instead, but keep in mind that it will contain much, much more than user-defined globals.

    0 讨论(0)
  • 2021-01-27 06:29

    Globals become attached to the global object, which in a browser is the window.

    a = 123
    

    is the same as

    window.a = 123
    
    0 讨论(0)
  • 2021-01-27 06:41

    How about:

    var globals = window || global;
    for (var key in globals){
      console.log(window[key]);
    }
    
    0 讨论(0)
提交回复
热议问题