# declare a function's name without creating a variable

**URL:** <https://es.discourse.group/t/declare-a-functions-name-without-creating-a-variable/2322>\
**Category:** 💡 Ideas\
**Created:** [March 11, 2025, 4:51am UTC](https://es.discourse.group/t/declare-a-functions-name-without-creating-a-variable/2322 "2025-03-11T04:51:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nektro](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nektro/32/1888_2.png) [@nektro](https://es.discourse.group/u/nektro)\
**Post date:** [March 11, 2025, 4:51am UTC](https://es.discourse.group/t/declare-a-functions-name-without-creating-a-variable/2322/1 "2025-03-11T04:51:02Z")

</div>

```js
const x = {};
x.y = function y() {
  console.log("hello");
  y();
};
x.y();

```

as written, calling `x.y()` will infinitely recurse. this is particularly cumbersome if there is a `y` in the outer scope as it is now unreachable:

```js
const x = {};
x.y = function y() {
  console.log("hello");
  y();
};
function y() {
  console.log("world");
}
x.y();

```

the only way to avoid it is like so:

```js
x.y = function () {
  console.log("hello");
  y();
};
Object.defineProperty(x.y, "name", { value: "y" });

```

curious on other's thoughts on how they handle this, or run into it often.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [March 11, 2025, 1:23pm UTC](https://es.discourse.group/t/declare-a-functions-name-without-creating-a-variable/2322/2 "2025-03-11T13:23:33Z")

</div>

I haven't found myself in the situation where the function name shadows an outer variable very often, but if I did I would probably create an outer alias to that variable:

```javascript
const x = {};
const outerY = y;
x.y = function y() {
  console.log("hello");
  outerY();
};
x.y();

```
