# functionName()

{% tabs %}
{% tab title="💾 程式" %}
* replit ⟩ [JS ⟩ function ⟩ functionName()](https://replit.com/@pegasusroe/JS-function-functionName#index.js)

```javascript
/**
 * functionName()
 * --------------
 * @returns {string} **current function name** inside the function itself.
 */
function functionName() {
    return functionName.caller.name;
}
```

💈範例：

```javascript
// free function
function test() {
    return functionName();  // caller === test
}

// methods within object
const o = {
    method1() {
        return functionName();     // caller === method1
    },
    method2() {
        return functionName();     // caller === method2
    }
};

// test code
test(),                 // 'test'
o.method1(),            // 'method1'
o.method2(),            // 'method2'
```
{% endtab %}

{% tab title="📘 手冊" %}
* MDN ⟩ [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference) ⟩ [built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) ⟩ [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) ⟩ [Function.name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name)
* JSDoc ⟩ [return value](https://jsdoc.app/tags-returns.html) (<mark style="color:yellow;">`@returns`</mark>)
{% endtab %}
{% endtabs %}
