Skip to content

Files

Latest commit

 

History

History

function-name

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Function Name

Determine a function's name.

## Usage
var functionName = require( '@stdlib/utils/function-name' );

functionName( fcn )

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 );
}
## Notes
  • For more information regarding the naming of anonymous functions, see
    • [Function Names in ES6][2ality]
    • Webkit bug [7726][webkit-bug-7726]
    • [MDN][mdn]
## Examples
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'
[2ality]: http://www.2ality.com/2015/09/function-names-es6.html [webkit-bug-7726]: https://bugs.webkit.org/show_bug.cgi?id=7726 [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name