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.
在Chrome的控制台中,我只需输入此内容即可获取所有密钥,包括字符串,数字等。
However when I do this in Node.js I get much less:
但是,当我在Node.js中执行此操作时,我得到的更少:
> 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?
this.eval来自哪里?
3 个解决方案
#1
5
The built-in properties of the global object are non-enumerable, so Object.keys
doesn't return them. You can use Object.getOwnPropertyNames
instead.
全局对象的内置属性是不可枚举的,因此Object.keys不会返回它们。您可以使用Object.getOwnPropertyNames。
#2
4
The following globals()
function will get you global namespace object:
以下globals()函数将获取全局命名空间对象:
function globals() { return this; }
With it you can list all variables of global namespace anytime you want:
有了它,您可以随时列出全局命名空间的所有变量:
function varsList() {
return Object.getOwnPropertyNames(globals());
}
#3
1
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.
您可以使用Object.getOwnPropertyNames(this)。如果没有将“this”作为参数或参数引用Object owner的属性,则getOwnPropertyNames()函数将不返回任何内容。
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
回答关于评估来自何处的问题,请查看此链接。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
#1
5
The built-in properties of the global object are non-enumerable, so Object.keys
doesn't return them. You can use Object.getOwnPropertyNames
instead.
全局对象的内置属性是不可枚举的,因此Object.keys不会返回它们。您可以使用Object.getOwnPropertyNames。
#2
4
The following globals()
function will get you global namespace object:
以下globals()函数将获取全局命名空间对象:
function globals() { return this; }
With it you can list all variables of global namespace anytime you want:
有了它,您可以随时列出全局命名空间的所有变量:
function varsList() {
return Object.getOwnPropertyNames(globals());
}
#3
1
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.
您可以使用Object.getOwnPropertyNames(this)。如果没有将“this”作为参数或参数引用Object owner的属性,则getOwnPropertyNames()函数将不返回任何内容。
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
回答关于评估来自何处的问题,请查看此链接。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval