是否有一种方法来获得目前在javascript范围内的所有变量?
当前回答
虽然每个人都回答“不”,我知道“不”是正确的答案,但如果你真的需要得到一个函数的局部变量,有一个有限的方法。
考虑这个函数:
var f = function() {
var x = 0;
console.log(x);
};
你可以把你的函数转换成一个字符串:
var s = f + '';
你会得到一个字符串形式的source of function
'function () {\nvar x = 0;\nconsole.log(x);\n}'
现在可以使用esprima这样的解析器来解析函数代码并查找局部变量声明。
var s = 'function () {\nvar x = 0;\nconsole.log(x);\n}';
s = s.slice(12); // to remove "function () "
var esprima = require('esprima');
var result = esprima.parse(s);
并找到对象:
obj.type == "VariableDeclaration"
在结果中(我已经删除了console.log(x)下面):
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "x"
},
"init": {
"type": "Literal",
"value": 0,
"raw": "0"
}
}
],
"kind": "var"
}
]
}
我已经在Chrome, Firefox和Node中进行了测试。
但是这个方法的问题是你只有函数本身定义的变量。举个例子:
var g = function() {
var y = 0;
var f = function() {
var x = 0;
console.log(x);
};
}
你只能得到x,而不是y。 但是仍然可以在循环中使用调用者链(arguments.callee.caller.caller.caller)来查找调用者函数的局部变量。如果你有所有的局部变量名,那么你就有了作用域变量。通过变量名,您可以通过简单的eval访问值。
其他回答
是也不是。几乎在任何情况下都说“不”。“是的”,但如果您想检查全局作用域,则只能以有限的方式进行。举个例子:
var a = 1, b = 2, c = 3;
for ( var i in window ) {
console.log(i, typeof window[i], window[i]);
}
在150多个其他东西中,输出如下:
getInterface function getInterface()
i string i // <- there it is!
c number 3
b number 2
a number 1 // <- and another
_firebug object Object firebug=1.4.5 element=div#_firebugConsole
"Firebug command line does not support '$0'"
"Firebug command line does not support '$1'"
_FirebugCommandLine object Object
hasDuplicate boolean false
因此,可以在当前范围内列出一些变量,但它不可靠、不简洁、不高效或不容易访问。
一个更好的问题是,为什么要知道作用域内的变量是什么?
你不能。
变量,函数声明的标识符和函数代码的参数,被绑定为变量对象的属性,这是不可访问的。
参见:
作用域链和标识符解析
虽然每个人都回答“不”,我知道“不”是正确的答案,但如果你真的需要得到一个函数的局部变量,有一个有限的方法。
考虑这个函数:
var f = function() {
var x = 0;
console.log(x);
};
你可以把你的函数转换成一个字符串:
var s = f + '';
你会得到一个字符串形式的source of function
'function () {\nvar x = 0;\nconsole.log(x);\n}'
现在可以使用esprima这样的解析器来解析函数代码并查找局部变量声明。
var s = 'function () {\nvar x = 0;\nconsole.log(x);\n}';
s = s.slice(12); // to remove "function () "
var esprima = require('esprima');
var result = esprima.parse(s);
并找到对象:
obj.type == "VariableDeclaration"
在结果中(我已经删除了console.log(x)下面):
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "x"
},
"init": {
"type": "Literal",
"value": 0,
"raw": "0"
}
}
],
"kind": "var"
}
]
}
我已经在Chrome, Firefox和Node中进行了测试。
但是这个方法的问题是你只有函数本身定义的变量。举个例子:
var g = function() {
var y = 0;
var f = function() {
var x = 0;
console.log(x);
};
}
你只能得到x,而不是y。 但是仍然可以在循环中使用调用者链(arguments.callee.caller.caller.caller)来查找调用者函数的局部变量。如果你有所有的局部变量名,那么你就有了作用域变量。通过变量名,您可以通过简单的eval访问值。
正如大家所注意到的:你不能。 但是你可以创建一个obj并将你声明的每个var赋值给那个obj。 这样你就可以很容易地检查你的vars:
var v = {}; //put everything here
var f = function(a, b){//do something
}; v.f = f; //make's easy to debug
var a = [1,2,3];
v.a = a;
var x = 'x';
v.x = x; //so on...
console.log(v); //it's all there
访问特定范围内Vars的最简单方法
打开开发人员工具>资源(Chrome) 打开具有该范围的函数的文件(提示cmd/ctrl+p来查找文件) 在该函数中设置断点并运行代码 当它停在断点时,您可以通过控制台(或范围变量窗口)访问范围变量。
注意:你想对未最小化的js做这个操作。
显示所有非私有变量的最简单方法
打开控制台(Chrome) 类型:this.window 回车
现在你将看到一个对象树,你可以展开所有已声明的对象。