Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Function:name

From MDC

Non-standard

[edit] Summary

The name of the function.

[edit] Description

The name property returns the name of a function, or an empty string for anonymous functions:

function doSomething() {}
alert(doSomething.name); // alerts "doSomething"

Note that in these examples anonymous functions are created, so name returns an empty string:

var f = function() { };
var object = {
  someMethod: function() {}
};

alert(f.name == ""); // true
alert(object.someMethod.name == ""); // also true

You can define a function with a name in a function expression:

var object = {
  someMethod: function object_someMethod() {}
};
alert(object.someMethod.name); // alerts "object_someMethod"

try { object_someMethod } catch(e) { alert(e); }
// ReferenceError: object_someMethod is not defined

You cannot change the name of a function, this property is read-only:

var object = {
  // anonymous
  someMethod: function(){}
};
object.someMethod.name = "someMethod";
alert(object.someMethod.name); // empty string, someMethod is anonymous

[edit] Examples

You can use obj.constructor.name to check the "class" of an object:

function a()
{
}

var b = new a();
alert(b.constructor.name); //Alerts "a"