mozilla

Revision 1051 of Function.name

  • Revision slug: JavaScript/Reference/Global_Objects/Function/name
  • Revision title: name
  • Revision id: 1051
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

{{ Non-standard_header() }}

Summary

The name of the function.

Property of Function
Implemented in JavaScript ?
ECMAScript Edition None

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

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"

Revision Source

<p>{{ Non-standard_header() }}</p>
<h2>Summary</h2>
<p>The name of the function.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Property of <a href="/en/JavaScript/Reference/Global_Objects/Function" title="en/JavaScript/Reference/Global_Objects/Function"><code>Function</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript ?</td> </tr> <tr> <td>ECMAScript Edition</td> <td>None</td> </tr> </tbody>
</table>
<h2>Description</h2>
<p>The name property returns the name of a function, or an empty string for anonymous functions:</p>
<pre class="eval">function doSomething() {}
alert(doSomething.name); // alerts "doSomething"
</pre>
<p>Note that in these examples anonymous functions are created, so <code>name</code> returns an empty string:</p>
<pre class="eval">var f = function() { };
var object = {
  someMethod: function() {}
};

alert(f.name == ""); // true
alert(object.someMethod.name == ""); // also true
</pre>
<p>You can define a function with a name in a <a href="/En/Core_JavaScript_1.5_Reference/Functions#Function_constructor_vs._function_declaration_vs._function_expression" title="En/Core_JavaScript_1.5_Reference/Functions#Function_constructor_vs._function_declaration_vs._function_expression">function expression</a>:</p>
<pre class="eval">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
</pre>
<p>You cannot change the <code>name</code> of a function, this property is read-only:</p>
<pre class="eval">var object = {
  // anonymous
  someMethod: function(){}
};
object.someMethod.name = "someMethod";
alert(object.someMethod.name); // empty string, someMethod is anonymous
</pre>
<h2>Examples</h2>
<p>You can use <code>obj.constructor.name</code> to check the "class" of an object:</p>
<pre class="eval">function a()
{
}

var b = new a();
alert(b.constructor.name); //Alerts "a"
</pre>
Revert to this revision