列出Node.js中的所有全局variables
我试图列出所有的全局variables,包括那些引用内置对象 。
在Chrome的控制台中,我可以简单地键入this
键,并取回所有的键,包括String
, Number
等等。
但是,当我在Node.js中做到这一点时,我得到了更less:
> Object.keys(this) [ 'global', 'process', 'GLOBAL', 'root', 'Buffer', 'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval', 'setImmediate', 'clearImmediate', 'console', 'module', 'require', '_' ] > this.eval [Function: eval]
this.eval
从哪里来?
全局对象的内置属性是不可枚举的,所以Object.keys
不会返回它们。 您可以改用Object.getOwnPropertyNames
。
下面的globals()
函数将得到你的全局命名空间对象:
function globals() { return this; }
有了它,您可以随时列出全局命名空间的所有variables:
function varsList() { return Object.getOwnPropertyNames(globals()); }