是否有一种方法来获得目前在javascript范围内的所有变量?


当前回答

你有多少时间?

如果你ḧ̸̬̦̒͑͝ͅa̵͍͖t̴̪̟̬̖̾̋͗̏ͅe̸̳̍͌͝你的cpu,你可以通过每一个有效的变量名,并评估每一个,看看它是否会产生一个值!

下面的代码段尝试了前1000个bruteforce字符串,这足以在作用域内找到虚构的变量名:

let alpha = 'abcdefghijklmnopqrstuvwxyz'; let everyPossibleString = function*() { yield ''; for (let prefix of everyPossibleString()) for (let char of alpha) yield `${prefix}${char}`; }; let allVarsInScope = (iterations=1000) => { let results = {}; let count = 0; for (let bruteforceString of everyPossibleString()) { if (!bruteforceString) continue; // Skip the first empty string try { results[bruteforceString] = eval(bruteforceString); } catch(err) {} if (count++ > iterations) break; } return results; }; let myScope = (() => { let dd = 'ddd'; let ee = 'eee'; let ff = 'fff'; ((gg, hh) => { // We can't call a separate function, since that function would be outside our // scope and wouldn't be able to see any variables - but we can define the // function in place (using `eval(allVarsInScope.toString())`), and then call // that defined-in-place function console.log(eval(allVarsInScope.toString())()); })('ggg', 'hhh'); })();

这个脚本将最终(经过很长时间)找到所有作用域变量名称,以及abc nifty和swell,我创建的一些示例变量。注意,它只会找到由字母组成的变量名。

let preElem = document.getElementsByClassName('display')[0]; let statusElem = document.getElementsByClassName('status')[0]; let alpha = 'abcdefghijklmnopqrstuvwxyz'; alpha += alpha.toUpperCase(); let everyPossibleString = function*() { yield ''; for (let prefix of everyPossibleString()) for (let char of alpha) yield `${prefix}${char}`; }; (async () => { let abc = 'This is the ABC variable :-|'; let neato = 'This is the NEATO variable :-)'; let swell = 'This is the SWELL variable :-D'; let results = {}; let batch = 25000; let waitMs = 25; let count = 0; let startStr = null; for (let bruteStr of everyPossibleString()) { try { if (bruteStr === '') continue; if (startStr === null) startStr = bruteStr; try { results[bruteStr] = eval(bruteStr); } catch(err) {} if (count++ >= batch) { statusElem.innerHTML = `Did batch of ${batch} from ${startStr} -> ${bruteStr}`; preElem.innerHTML = JSON.stringify(results, null, 2); count = 0; startStr = null; await new Promise(r => setTimeout(r, waitMs)); } } catch(err) { // It turns out some global variables are protected by stackoverflow's snippet // system (these include "top", "self", and "this"). If these values are touched // they result in a weird iframe error, captured in this `catch` statement. The // program can recover by replacing the most recent `result` value (this will be // the value which causes the error). let lastEntry = Object.entries(results).slice(-1)[0]; results[lastEntry[0]] = '<a protected value>'; } } console.log('Done...'); // Will literally never happen })(); html, body { position: fixed; left: 0; top: 0; right: 0; bottom: 0; margin: 0; padding: 0; overflow: hidden } .display { position: fixed; box-sizing: border-box; left: 0; top: 0; bottom: 30px; right: 0; overflow-y: scroll; white-space: pre; font-family: monospace; padding: 10px; box-shadow: inset 0 0 10px 1px rgba(0, 0, 0, 0.3); } .status { position: fixed; box-sizing: border-box; left: 0; bottom: 0px; right: 0; height: 30px; line-height: 30px; padding: 0 10px; background-color: rgba(0, 0, 0, 1); color: rgba(255, 255, 255, 1); font-family: monospace; } <div class="display"></div> <div class="status"></div>

我非常清楚,这在任何情况下都是不现实的

其他回答

访问特定范围内Vars的最简单方法

打开开发人员工具>资源(Chrome) 打开具有该范围的函数的文件(提示cmd/ctrl+p来查找文件) 在该函数中设置断点并运行代码 当它停在断点时,您可以通过控制台(或范围变量窗口)访问范围变量。

注意:你想对未最小化的js做这个操作。

显示所有非私有变量的最简单方法

打开控制台(Chrome) 类型:this.window 回车

现在你将看到一个对象树,你可以展开所有已声明的对象。

你不能。

变量,函数声明的标识符和函数代码的参数,被绑定为变量对象的属性,这是不可访问的。

参见:

作用域链和标识符解析

您可以在[[scopes]]中看到作用域及其变量,甚至可以使用console.dir()查看闭包作用域。

示例1:

counterWithClosure = (function () {
    let internalVar = 0
    return function () {
        return ++internalVar
    }
})()

counterWithClosure() // 1
counterWithClosure() // 2
counterWithClosure() // 3

console.dir(counterWithClosure)

它会显示“[[Scopes]] > Closure”,“[[Scopes]] > Global”,甚至“[[Scopes]] > Script”中的变量,如果页面有脚本可达的话。

图片输出:

即使使用嵌套闭包,也可以看到嵌套作用域。

示例2:

adderWithNestedClosures = (function () {
    let varLevel1 = 1
    return function (param1) {
        let varLevel2 = 10
        return function (param2) {
            let varLevel3 = 100
            return function (param3) {
                let varLevel4 = 1000
                return function (param4) {
                    ++varLevel1
                    ++varLevel2
                    ++varLevel3
                    ++varLevel4
                    return {
                        paramsSum: param1 + param2 + param3 + param4,
                        varsSum: varLevel1 + varLevel2 + varLevel3 + varLevel4
                    }
                }
            }
        }
    }
})()

adderWith123 = adderWithNestedClosures(1)(2)(3) // Preparing function with nested scopes
adderWith123(4) // {paramsSum:10,varsSum:1115}
adderWith123(4) // {paramsSum:10,varsSum:1119}
adderWith123(5) // {paramsSum:11,varsSum:1123}
console.dir(adderWith123) 

它显示了[[scopes]]中的所有嵌套作用域 图片输出:

它适用于基于chromium的浏览器。

我做了一个小提琴实现(本质上)以上的想法概述iman。下面是当你把鼠标放在第二个ipsum上,返回ipsum*ipsum -…

范围内的变量在声明的地方突出显示(不同的范围使用不同的颜色)。带红色边界的lorem是一个阴影变量(不在范围内,但如果树下的另一个lorem不在那里,那么它就在范围内)。

我使用esprima库来解析JavaScript,以及estraverse, escodegen, escope(在esprima之上的实用程序库)。“繁重的工作”全部由这些库完成(当然,最复杂的是esprima本身)。

它是如何工作的

ast = esprima.parse(sourceString, {range: true, sourceType: 'script'});

生成抽象语法树。然后,

analysis = escope.analyze(ast);

生成一个复杂的数据结构,其中封装有关程序中所有作用域的信息。其余的工作是收集该分析对象(以及抽象语法树本身)中编码的信息,并从中生成交互式着色方案。

所以正确的答案实际上不是“不”,而是“是的,但是”。“但是”是一个很大的问题:你基本上必须用JavaScript重写chrome浏览器(以及devtools)的重要部分。JavaScript是一种图灵完备语言,所以原则上这当然是可能的。不可能的是不使用源代码的全部(作为字符串),然后用它来做非常复杂的事情。

正如大家所注意到的:你不能。 但是你可以创建一个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