gpt4 book ai didi

javascript - 如何跟踪 Javascript 事件(Stack Trace)?

转载 作者:数据小太阳 更新时间:2023-10-29 03:48:58 27 4
gpt4 key购买 nike

在任何编程语言中,我都可以跟踪任何函数并知道其他人调用了哪个函数。但是在 Javascript 中,我不知道怎么做,因为代码不是我写的,而且据我所知,Firebug 没有提供这个功能。

一个例子:

我想显示点击XYZ元素时调用的每个函数的函数名,并按顺序显示。

最佳答案

找到这个:A javascript stacktrace in any browser , James说他们有一个github account now

function printStackTrace() {
var callstack = [];
var isCallstackPopulated = false;
try {
i.dont.exist+=0; //doesn't exist- that's the point
} catch(e) {
if (e.stack) { //Firefox
var lines = e.stack.split('\n');
for (var i=0, len=lines.length; i<len; i++) {
if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
callstack.push(lines[i]);
}
}
//Remove call to printStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
else if (window.opera && e.message) { //Opera
var lines = e.message.split('\n');
for (var i=0, len=lines.length; i<len; i++) {
if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
var entry = lines[i];
//Append next line also since it has the file info
if (lines[i+1]) {
entry += " at " + lines[i+1];
i++;
}
callstack.push(entry);
}
}
//Remove call to printStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
}
if (!isCallstackPopulated) { //IE and Safari
var currentFunction = arguments.callee.caller;
while (currentFunction) {
var fn = currentFunction.toString();
var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf('')) || 'anonymous';
callstack.push(fname);
currentFunction = currentFunction.caller;
}
}
output(callstack);
}

function output(arr) {
// Output however you want
alert(arr.join('\n\n'));
}

关于javascript - 如何跟踪 Javascript 事件(Stack Trace)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3283576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com