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' );
console.log( functionName( Math.sqrt ) );
// returns 'sqrt'
console.log( functionName( Float64Array ) );
// returns 'Float64Array'
console.log( functionName( Buffer ) );
// returns 'Buffer'
console.log( functionName( Date ) );
// returns 'Date'
console.log( functionName( String ) );
// returns 'String'
console.log( functionName( Boolean ) );
// returns 'Boolean'
console.log( functionName( Function ) );
// returns 'Function'
console.log( functionName( Number ) );
// returns 'Number'
console.log( functionName( function foo(){} ) );
// returns 'foo'
console.log( functionName( function(){} ) );
// returns '' || 'anonymous'