开发者

Javascript backtrace

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, w开发者_开发技巧hich 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.


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);


Another way is to use console.trace();

source: https://developer.mozilla.org/en-US/docs/Web/API/Console/trace


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

Example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜