Function.name

Did you know that you can read content offline by using one of these tools? If you would like to read offline MDN content in another format, let us know by commenting on Bug 665750.

Dash App

Redirected from Function.name Redirect 1

Table of Contents

  1. Summary
  2. Description
  3. Examples

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

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"

Tags (2)