Posted By

Sephr on 02/23/09


Tagged

function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

enajenkins


Get Function Name


 / Published in: JavaScript
 

Get Function Name

This will only return (a string) if the object passed to getFunctionName is a function or an "object" function from IE. The function does not rely on function.name if present as it can't always be trusted.

Examples:

getFunctionName(function Foo(){}) == "Foo"
getFunctionName(function(){}) == ""
  1. function getFunctionName(func) {
  2. if ( typeof func == "function" || typeof func == "object" )
  3. var fName = (""+func).match(
  4. /function\s*([\w\$]*)\s*\(/
  5. ); if ( fName !== null ) return fName[1];
  6. }

Report this snippet  

You need to login to post a comment.