Javascript backtrace

匆匆过客 提交于 2019-12-10 03:26:14

问题


How to I get a backtrace in Javascript?

Ideal features:

  • entry function name, or some meaningful identifier for anonymous functions,
  • argument list at each level,
  • line numbers.

Can this be done in standard ECMAScript?

If not, can it be done in the common web browser dialects?

Thanks.

Edit --

Thanks for your suggestions.

My dialect doesnot support arguments.caller or arguments.callee.

I can do this:

try {
    let x = null;
    x .foo ();
}
catch (e) {
        debug (dump (e.stack));
}

Which gets me the information as a string, which is okay for at-a-glance, but it would be a great help to walk e.stack. Does it have a standard form?

Thanks again.


回答1:


maybe this might help you, haven't worked with it yet though:

This link is no longer active: kallewoof.com/2006/03/15/precompiling-javascript-functions/

also have a look at this:

How do you find out the caller function in JavaScript?




回答2:


In my debugging experience, I always use this

(function () { console.log(new Error().stack); })();

As the comments below suggested, the readers should prefer: console.log(new Error().stack);




回答3:


This lib: stacktrace-js works anywhere, not only with errors.

Example:

StackTrace.get()
    .then(stack => { console.log(stack) })
    .catch(err => { console.log(err) });


来源:https://stackoverflow.com/questions/6787711/javascript-backtrace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!