README.md (3016B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Function Name 22 23 > Determine a function's name. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var functionName = require( '@stdlib/utils/function-name' ); 31 ``` 32 33 #### functionName( fcn ) 34 35 Returns the name of a `function`. 36 37 <!-- eslint-disable stdlib/no-builtin-math --> 38 39 ```javascript 40 var v = functionName( Math.sqrt ); 41 // returns 'sqrt' 42 ``` 43 44 If provided an **anonymous** `function`, the function returns an empty `string` or the string `"anonymous"`. 45 46 <!-- eslint-disable no-empty-function, func-names, no-restricted-syntax --> 47 48 ```javascript 49 var v = functionName( function () {} ); 50 // returns '' || 'anonymous' 51 ``` 52 53 If provided a value which is not a `function`, the function **throws** a `TypeError`. 54 55 ```javascript 56 try { 57 functionName( 'beep' ); 58 // throws error... 59 } catch ( err ) { 60 console.error( err ); 61 } 62 ``` 63 64 </section> 65 66 <!-- /.usage --> 67 68 <section class="notes"> 69 70 ## Notes 71 72 - For more information regarding the naming of anonymous functions, see 73 74 - [Function Names in ES6][2ality-function-names] 75 - Webkit bug [7726][webkit-bug-7726] 76 - [MDN][mdn-function-name] 77 78 </section> 79 80 <!-- /.notes --> 81 82 <section class="examples"> 83 84 ## Examples 85 86 <!-- eslint-disable no-empty-function, no-restricted-syntax, func-names, stdlib/no-builtin-math --> 87 88 <!-- eslint no-undef: "error" --> 89 90 ```javascript 91 var Float64Array = require( '@stdlib/array/float64' ); 92 var Buffer = require( '@stdlib/buffer/ctor' ); 93 var Number = require( '@stdlib/number/ctor' ); 94 var functionName = require( '@stdlib/utils/function-name' ); 95 96 var v = functionName( Math.sqrt ); 97 // returns 'sqrt' 98 99 v = functionName( Float64Array ); 100 // returns 'Float64Array' 101 102 v = functionName( Buffer ); 103 // returns 'Buffer' 104 105 v = functionName( Date ); 106 // returns 'Date' 107 108 v = functionName( String ); 109 // returns 'String' 110 111 v = functionName( Boolean ); 112 // returns 'Boolean' 113 114 v = functionName( Function ); 115 // returns 'Function' 116 117 v = functionName( Number ); 118 // returns 'Number' 119 120 v = functionName( function foo() {} ); 121 // returns 'foo' 122 123 v = functionName( function () {} ); 124 // returns '' || 'anonymous' 125 ``` 126 127 </section> 128 129 <!-- /.examples --> 130 131 <section class="links"> 132 133 [2ality-function-names]: http://www.2ality.com/2015/09/function-names-es6.html 134 135 [webkit-bug-7726]: https://bugs.webkit.org/show_bug.cgi?id=7726 136 137 [mdn-function-name]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name 138 139 </section> 140 141 <!-- /.links -->