Get the name of an object
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.travis.yml
README.md
UNLICENSE
gulpfile.babel.js
package.json

README.md

getName

GitHub version Build Status Size

Get the name of an object

Usage

Get the name of a function, class, or constructor:

function foo() {}
class Bar {}

getName(foo); // "foo"
getName(Bar); // "Bar"
getName(Array); // "Array"

Get the name of an instance's constructor:

function Foo() {}
class Bar {}

getName(new Foo()); // "Foo"
getName(new Bar()); // "Bar"
getName(new Array()); // "Array"

Support's the displayName property:

function foo() {}
foo.displayName = 'bar';

getName(foo); // "bar"

Anonymous functions and arrow functions return an empty string:

getName(function() {})); // ""
getName(() => {})); // ""

Beware

If you're using a JavaScript compressor such as UglifyJS, function names will be changed, resulting in unexpected behavior. You can disable this feature in UglifyJS by setting the mangle option to false.

Installation

getName is CommonJS and AMD compatible with no dependencies. You can download the development or minified version, or install it in one of the following ways:

npm install ryanmorr/get-name

bower install ryanmorr/get-name

Tests

Run unit tests in the command line by issuing the following commands:

npm install
npm install -g gulp
gulp test

License

This project is dedicated to the public domain as described by the Unlicense.