Skip to content

Instantly share code, notes, and snippets.

@abarrak
Forked from irisli/callerName.js
Created September 9, 2018 13:35
Show Gist options
  • Save abarrak/3968d01f046c21927b6d55c24181ecb7 to your computer and use it in GitHub Desktop.
Save abarrak/3968d01f046c21927b6d55c24181ecb7 to your computer and use it in GitHub Desktop.
JavaScript get caller in strict mode
"use strict";
var stackTrace = (new Error()).stack; // Only tested in latest FF and Chrome
var callerName = stackTrace.replace(/^Error\s+/, ''); // Sanitize Chrome
callerName = callerName.split("\n")[1]; // 1st item is this, 2nd item is caller
callerName = callerName.replace(/^\s+at Object./, ''); // Sanitize Chrome
callerName = callerName.replace(/ \(.+\)$/, ''); // Sanitize Chrome
callerName = callerName.replace(/\@.+/, ''); // Sanitize Firefox
console.log(callerName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment