Skip to content

Instantly share code, notes, and snippets.

@stewartmcgown
Created November 9, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stewartmcgown/0d898ed17b19218d4f087575a0809648 to your computer and use it in GitHub Desktop.
Save stewartmcgown/0d898ed17b19218d4f087575a0809648 to your computer and use it in GitHub Desktop.
Get an array of the JavaScript callstack
class test {
static isPrivate() {
let callername;
try {
throw new Error();
} catch (e) {
/** @type {String[]} */
let stack = e.stack.split("\n");
let out = [];
for (let i = 0; i < stack.length; i++) {
if (i == 0 || i == stack.length - 1) continue;
out.push(
stack[i]
.replace("at", "")
.replace(/ *\([^)]*\) */g, "")
.trim()
);
}
console.dir(out);
}
}
static one() {
this.isPrivate();
}
static two() {
this.one();
}
static three() {
this.two();
}
static four() {
this.three();
}
}
(initial = () => test.four())();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment