Function.name

  • Revision slug: JavaScript/Reference/Global_Objects/Function/name
  • Revision title: Function.name
  • Revision id: 327937
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

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"

Functions created with the syntax new Function(...) or just Function(...) have their name property set to "anonymous" on Firefox and Safari, or to an empty string on Chrome and Opera. This property is not supported on Internet Explorer.

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

<div>
  {{Non-standard_header}}</div>
<h2 id="Summary">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-US/docs/JavaScript/Reference/Global_Objects/Function" title="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 id="Description">Description</h2>
<p>The name property returns the name of a function, or an empty string for anonymous functions:</p>
<pre class="brush:js">
function doSomething() { }

alert(doSomething.name); // alerts "doSomething"
</pre>
<p>Functions created with the syntax <code>new <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="Function">Function</a>(...)</code> or just <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="Function">Function</a></code><code>(...)</code> have their name property set to "anonymous" on Firefox and Safari, or to an empty string on Chrome and Opera. This property is not supported on Internet Explorer.</p>
<p>Note that in these examples anonymous functions are created, so <code>name</code> returns an empty string:</p>
<pre class="brush:js">
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-US/docs/JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression" title="JavaScript/Reference/Functions_and_function_scope#Function_constructor_vs._function_declaration_vs._function_expression">function expression</a>:</p>
<pre class="brush:js">
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="brush:js">
var object = {
  // anonymous
  someMethod: function() { }
};

object.someMethod.name = "someMethod";
alert(object.someMethod.name); // empty string, someMethod is anonymous
</pre>
<h2 id="Examples">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