Determine a function's name.
var functionName = require( '@stdlib/utils/function-name' );
Returns the name of a function
.
var name = functionName( Math.sqrt )
// returns 'sqrt'
If provided an anonymous function
, the function returns an empty string
or the string "anonymous"
.
var name = functionName( function(){} );
// returns '' || 'anonymous'
If provided a value which is not a function
, the function throws a TypeError
.
try {
// throws...
var name = functionName( 'beep' );
} catch ( err ) {
console.error( err );
}
- For more information regarding the naming of anonymous functions, see
- Function Names in ES6
- Webkit bug 7726
- MDN
var functionName = require( '@stdlib/utils/function-name' );
var bool = functionName( Math.sqrt );
// returns 'sqrt'
bool = functionName( Float64Array );
// returns 'Float64Array'
bool = functionName( Buffer );
// returns 'Buffer'
bool = functionName( Date );
// returns 'Date'
bool = functionName( String );
// returns 'String'
bool = functionName( Boolean );
// returns 'Boolean'
bool = functionName( Function );
// returns 'Function'
bool = functionName( Number );
// returns 'Number'
bool = functionName( function foo(){} );
// returns 'foo'
bool = functionName( function(){} );
// returns '' || 'anonymous'