简体   繁体   English

比较堆栈跟踪(Chrome开发者工具)

[英]Compare stack traces (Chrome Developer Tools)

Let's say I have a JavaScript program which does something with an object and which I run twice, with different objects. 假设我有一个JavaScript程序,它对一个对象做了一些事情,我用不同的对象运行了两次。

Is it somehow possible to automatically compare/diff the stack traces of the two runs, when I debug the program using Chrome Developer Tools? 当我使用Chrome开发者工具调试程序时,是否可以以某种方式自动比较/区分两次运行的堆栈跟踪? The idea behind my request is that if I debug an unknown program, I want to know the difference if how the different objects are handled, eg at which point in the program the difference is detected. 我的请求背后的想法是,如果我调试一个未知的程序,我想知道如何处理不同的对象,例如在程序中的哪个点检测到差异。

If the program is big enough it can be very annoying to debug both runs manually and try to remember where the difference(s) happen(ed). 如果程序足够大,那么手动调试两个运行并尝试记住差异发生的位置(编辑)会非常烦人。

You need to specify the point in execution where the call stack should be ouput. 您需要指定执行中应该输出调用堆栈的点。 If both paths end in the same function, you can put a console.trace() call in the last one and you will get a stack output for both of them in the Console. 如果两个路径都在同一个函数中结束,则可以在最后一个路径中放入console.trace()调用,并在控制台中为它们获取堆栈输出。

Example: 例:

var objA = { type: "a", val: 1 };
var objB = { type: "b", val: 2 };

function doSomething() {
    console.log("do something");
    doFinalThing(); 
}

function doSomethingElse() {
    console.log("do something else");
    doFinalThing();
}

function doFinalThing() {
    console.trace();
}

function init(obj) {
    if (obj.type == "a") { 
        doSomething();
    } else { 
        doSomethingElse();
    }
}

init(objA);
init(objB);

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM