列出Node.js中的所有全局变量 [英] List all global variables in Node.js

查看:296
本文介绍了列出Node.js中的所有全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出所有全局变量,包括那些引用内置对象

I'm trying to list all of the global variables, including those refering to built-in objects.

在Chrome的控制台中,我只需键入即可返回所有键,包括 String Number 等等。

In Chrome's console I can simply type this and get back all the keys, including things like String, Number, etc.

然而,当我在Node.js中执行此操作时,我得到的更少:

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]

哪里是这个.eval 来自?

推荐答案

全局对象的内置属性是不可枚举的, Object.keys 不会返回它们。您可以使用 对象。改为getOwnPropertyNames

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

这篇关于列出Node.js中的所有全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆