Determine the name of a value's constructor.
var constructorName = require( '@stdlib/utils/constructor-name' );
Returns the name of a value's constructor.
var name = constructorName( 'a' );
// returns 'String'
name = constructorName( 5 );
// returns 'Number'
function Beep(){}
name = constructorName( new Beep() );
// returns 'Beep'
description | value | constructor | notes |
---|---|---|---|
string | 'beep' |
'String' |
|
number | 5 |
'Number' |
|
NaN | NaN |
'Number' |
|
infinity | +infinity /-infinity |
'Number' |
|
boolean | true /false |
'Boolean' |
|
null | null |
'Null' |
|
undefined | undefined |
'Undefined' |
|
array | ['beep', 5] |
'Array' |
|
object | {'foo': 'bar'} |
'Object' |
|
function | function (){} |
'Function' |
|
symbol | Symbol() |
'Symbol' |
ES2015 |
regexp | /./ |
'RegExp' |
Android 4.1+ |
String | new String('beep') |
'String' |
|
Number | new Number(5) |
'Number' |
|
Boolean | new Boolean(false) |
'Boolean' |
|
Object | new Object() |
'Object' |
|
Array | new Array() |
'Array' |
|
Int8Array | new Int8Array() |
'Int8Array' |
|
Uint8Array | new Uint8Array() |
'Uint8Array' |
|
Uint8ClampedArray | new Uint8ClampedArray() |
'Uint8ClampedArray' |
|
Int16Array | new Int16Array() |
'Int16Array' |
|
Uint16Array | new Uint16Array() |
'Uint16Array' |
|
Int32Array | new Int32Array() |
'Int32Array' |
|
Uint32Array | new Uint32Array() |
'Uint32Array' |
|
Float32Array | new Float32Array() |
'Float32Array' |
|
Float64Array | new Float64Array() |
'Float64Array' |
|
ArrayBuffer | new ArrayBuffer() |
'ArrayBuffer' |
|
Buffer | new Buffer() |
'Buffer' |
Node.js |
Date | new Date() |
'Date' |
|
RegExp | new RegExp('.') |
'RegExp' |
Android 4.1+ |
Function | new Function('x', 'return x') |
'Function' |
|
Map | new Map() |
'Map' |
ES2015 |
WeakMap | new WeakMap() |
'WeakMap' |
ES2015 |
Set | new Set() |
'Set' |
ES2015 |
WeakSet | new WeakSet() |
'WeakSet' |
ES2015 |
Error | new Error() |
'Error' |
|
TypeError | new TypeError() |
'TypeError' |
|
SyntaxError | new SyntaxError() |
'SyntaxError' |
|
ReferenceError | new ReferenceError() |
'ReferenceError' |
|
URIError | new URIError() |
'URIError' |
|
RangeError | new RangeError() |
'RangeError' |
|
EvalError | new EvalError() |
'EvalError' |
|
Math | Math |
'Math' |
|
JSON | JSON |
'JSON' |
IE8+ |
arguments | (function(){return arguments;})() |
'Arguments' |
IE9+ |
custom constructor | new Beep() |
'Beep' |
|
anonymous constructor | new (function(){})() |
'' |
- If a value's constructor is an anonymous
function
, the implementation returns an emptystring
.var Beep = function(){}; var name = constructorName( new Beep() ); // returns ''
var constructorName = require( '@stdlib/utils/constructor-name' );
console.log( constructorName( 'a' ) );
// returns 'String'
console.log( constructorName( 5 ) );
// returns 'Number'
console.log( constructorName( NaN ) );
// returns 'Number'
console.log( constructorName( null ) );
// returns 'Null'
console.log( constructorName( undefined ) );
// returns 'Undefined'
console.log( constructorName( true ) );
// returns 'Boolean'
console.log( constructorName( false ) );
// returns 'Boolean'
console.log( constructorName( {} ) );
// returns 'Object'
console.log( constructorName( [] ) );
// returns 'Array'
console.log( constructorName( function noop(){} ) );
// returns 'Function'
console.log( constructorName( /./ ) );
// returns 'RegExp'
console.log( constructorName( new Date() ) );
// returns 'Date'
console.log( constructorName( new Map() ) );
// returns 'Map'
console.log( constructorName( new WeakMap() ) );
// returns 'WeakMap'
console.log( constructorName( new Set() ) );
// returns 'Set'
console.log( constructorName( new WeakSet() ) );
// returns 'WeakSet'
console.log( constructorName( Symbol() ) );
// returns 'Symbol'
console.log( constructorName( new Error() ) );
// returns 'Error'
console.log( constructorName( new TypeError() ) );
// returns 'TypeError'
console.log( constructorName( new SyntaxError() ) );
// returns 'SyntaxError'
console.log( constructorName( new URIError() ) );
// returns 'URIError'
console.log( constructorName( new RangeError() ) );
// returns 'RangeError'
console.log( constructorName( new ReferenceError() ) );
// returns 'ReferenceError'
console.log( constructorName( new EvalError() ) );
// returns 'EvalError'
console.log( constructorName( new Int8Array() ) );
// returns 'Int8Array'
console.log( constructorName( new Uint8Array() ) );
// returns 'Uint8Array'
console.log( constructorName( new Uint8ClampedArray() ) );
// returns 'Uint8ClampedArray'
console.log( constructorName( new Int16Array() ) );
// returns 'Int16Array'
console.log( constructorName( new Uint16Array() ) );
// returns 'Uint16Array'
console.log( constructorName( new Int32Array() ) );
// returns 'Int32Array'
console.log( constructorName( new Uint32Array() ) );
// returns 'Uint32Array'
console.log( constructorName( new Float32Array() ) );
// returns 'Float32Array'
console.log( constructorName( new Float64Array() ) );
// returns 'Float64Array'
console.log( constructorName( new ArrayBuffer() ) );
// returns 'ArrayBuffer'
console.log( constructorName( new Buffer( 'beep' ) ) );
// returns 'Buffer'
console.log( constructorName( Math ) );
// returns 'Math'
console.log( constructorName( JSON ) );
// returns 'JSON'
function Person(){}
console.log( constructorName( new Person() ) );
// returns 'Person'
var Person = function(){};
console.log( constructorName( new Person() ) );
// returns ''