stacktrace() Function for JavaScript Exceptions
Determining a JavaScript stack trace is accessible when JavaScript exceptions occur. However, acquiring a stack trace for custom exceptions thrown manually may require specific techniques.
For custom exceptions, the following script can be employed:
function stacktrace() { function st2(f) { return !f ? [] : st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']); } return st2(arguments.callee.caller); }
Additionally, modern browsers offer the console.trace() function for capturing stack traces during debugging.
Updated Solution (2013):
A simpler approach is to utilize the Error object's stack property:
function stackTrace() { var err = new Error(); return err.stack; }
The above is the detailed content of How to Obtain Stack Trace Information in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!