Skip to content

Instantly share code, notes, and snippets.

@PAEz
Forked from dfkaye/js-get-fn-name.js
Last active August 29, 2015 14:17
Show Gist options
  • Save PAEz/838aee38f49bb7248bc7 to your computer and use it in GitHub Desktop.
Save PAEz/838aee38f49bb7248bc7 to your computer and use it in GitHub Desktop.
function getFnName(fn) {
var f = typeof fn == 'function';
var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/));
return (!f && 'not a function') || (s && s[1] || 'anonymous');
}
console.log(getFnName(String)); // 'String'
console.log(getFnName(function test(){})); // 'test'
console.log(getFnName(function (){})); // 'anonymous'
console.log(getFnName(function(){})); // 'anonymous'
console.log(getFnName(Function())); // 'anonymous'
console.log(getFnName({name: 'test'})); // 'not a function'
console.log(getFnName('test')); // 'not a function'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment