Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

javascript - 获取范围内的所有变量(Getting All Variables In Scope)

有没有办法获取当前在javascript中作用域内的所有变量?

  ask by Ian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Although everyone answer " No " and I know that "No" is the right answer but if you really need to get local variables of a function there is a restricted way.

(尽管每个人都回答“ ”,并且我知道“否”是正确的答案,但是如果您确实需要获取函数的局部变量 ,则有一种受限的方法。)

Consider this function:

(考虑以下功能:)

var f = function() {
    var x = 0;
    console.log(x);
};

You can convert your function to a string:

(您可以将函数转换为字符串:)

var s = f + '';

You will get source of function as a string

(您将以字符串形式获取函数源)

'function () {
var x = 0;
console.log(x);
}'

Now you can use a parser like esprima to parse function code and find local variable declarations.

(现在,您可以使用诸如esprima之类的解析器来解析功能代码并查找局部变量声明。)

var s = 'function () {
var x = 0;
console.log(x);
}';
s = s.slice(12); // to remove "function () "
var esprima = require('esprima');
var result = esprima.parse(s);

and find objects with:

(并找到具有以下内容的对象:)

obj.type == "VariableDeclaration"

in the result (I have removed console.log(x) below):

(结果(我在下面删除了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"
        }
    ]
}

I have tested this in Chrome, Firefox and Node.

(我已经在Chrome,Firefox和Node中对此进行了测试。)

But the problem with this method is that you just have the variables defined in the function itself.

(但是这种方法的问题在于,您仅具有在函数本身中定义的变量。)

For example for this one:

(例如此示例:)

var g = function() {
    var y = 0;
    var f = function() {
        var x = 0;
        console.log(x);
    };
}

you just have access to the x and not y .

(您只能访问x而不是y 。)

But still you can use chains of caller (arguments.callee.caller.caller.caller) in a loop to find local variables of caller functions.

(但是仍然可以在循环中使用调用方链(arguments.callee.caller.caller.caller)查找调用方函数的局部变量。)

If you have all local variable names so you have scope variables .

(如果您具有所有局部变量名称,那么就具有范围变量 。)

With the variable names you have access to values with a simple eval.

(使用变量名,您可以通过简单的eval访问值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...