It's quite easy. If you don't have an exception, create one. Then you can print the stack trace off of it. Like this (gist) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(args) { | |
var i, j, k; | |
// ... | |
// j acquires some interesting value | |
// Who called foo when j took this interesting value? | |
// | |
var e = new Error('dummy'); | |
var stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '') | |
.replace(/^\s+at\s+/gm, '') | |
.replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@') | |
.split('\n'); | |
console.log(stack); | |
// ... | |
// rest of the code | |
} |
2 comments:
You can easily get a stack trace at any time by calling console.trace() in your Javascript or in the Firebug console.
here's a similar post: http://www.eriwen.com/javascript/js-stack-trace/
Great tip for when console is not available... but 'stack' doesn't appear to be a standard property (only on Mozilla?)
Post a Comment