nameof
Rather than just the type of an object, get its name. typeof 42
is "number"
, just like one would expect, but typeof []
and typeof new Date
are both just "object". nameof gets the true name of an object.
Examples
var nameof = require('nameof'); nameof(42); // "Number"nameof([]); // "Array"nameof({}); // "Object"nameof(new Date()); // "Date" function Happy () { this.youKnowIt = true;}nameof(Happy); // "Function"nameof(new Happy); // "Happy"
Caveats
Some custom objects, such as those created by anonymous constructors or the Revealing Module Pattern have no specific name.
var Clap = function () { this.yourHands = true;}nameof(Clap); // "Function"nameof(new Clap); // "" function Hokey() { function pokey () { return 'Turn around!'; } return { pokey: pokey };}nameof(new Hokey); // "Object"
Usage
npm install --save nameof
In a source file, require
the module and start using it.
var nameof = require('nameof');nameof(function Shake() {}); // "Function"
License
Licensed under the BSD 3-Clause License, the full text of which can be read in LICENSE.