Return to Snippet

Revision: 11882
at April 19, 2009 23:58 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || typeof func == "object" )
  var fName = (""+func).match(
    /function\s*([\w\$]*)\s*\(/
  ); if ( fName !== null ) return fName[1];
}

Revision: 11881
at February 25, 2009 15:18 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || typeof func == "object" )
  var fName = (""+func).match(
    /^function\s*([\w\$]*)\s*\(/
  ); if ( fName !== null ) return fName[1];
}

Revision: 11880
at February 24, 2009 15:19 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((""+func).replace(RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), "\\$&"))).test(""+func.toString)) )
  var fName = (""+func).match(
    /^function\s*([\w$]*)\s*\(/
  ); if ( fName !== null ) return fName][1];
};

Revision: 11879
at February 24, 2009 15:14 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((""+func).replace(RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), "\\$&"))).test(""+func.toString)) )
  var fName = (""+func).match(
    /^function\s*([\w$]*)\s*\(/
  ); if ( fName !== null ) return fName;
};

Revision: 11878
at February 23, 2009 15:30 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((""+func).replace(RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), "\\$&"))).test(""+func.toString)) )
  return (""+func).match(
    /^function\s*([\w$]*)\s*\(/
  )[1];
};

Revision: 11877
at February 23, 2009 15:27 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((""+func).replace(RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), "\\$&"))).test(""+func.toString))  )
  return (""+func).match(
    /^function\s*([\w$]*)\s*\(/
  )[1];
};

Revision: 11876
at February 23, 2009 15:18 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((function (str) {
    var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\ 
    return str.replace(specials, "\\$&");
  })(""+func)).test(""+func.toString) ))  )

  return (""+func).match(
    /^function\s*([\w$]*)\s*\(/
  )[1];
};

Revision: 11875
at February 23, 2009 15:15 by Sephr


Updated Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((function (str) {
    var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\ 
    return str.replace(specials, "\\$&");
  })(""+func)).test(""+func.toString) ))  )

  return (""+func).match(
    /function\s*([\w$]*)\s*\(/
  )[1];
};

Revision: 11874
at February 23, 2009 00:39 by Sephr


Initial Code
function getFunctionName(func) {
  if ( typeof func == "function" || (typeof func == "object" && !( RegExp((function (str) {
    var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\ 
    return str.replace(specials, "\\$&");
  })(""+func)).test(""+func.toString) ))  )

  var funcName = (""+func).match(
    /function\s*([\w$]*)\s*\(/
  );
  if ( typeof funcName[1] != "undefined" ) return funcName[1];
};

Initial URL


Initial Description
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(){}) == ""

Initial Title
Get Function Name

Initial Tags
function

Initial Language
JavaScript