# Is there an idea to add function definition in an object literal or array literal?

**URL:** <https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140>\
**Category:** 💡 Ideas\
**Created:** [December 30, 2021, 5:09am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140 "2021-12-30T05:09:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [December 30, 2021, 5:09am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/1 "2021-12-30T05:09:02Z")

</div>

For instance, the following `external` method can be accessed in the same scope of the `obj`.

```javascript
console.log(external); // => ƒ external() {}

const obj = {
    a : 1,
    function external() {
       
    },
    b : 2,
};

console.log(external); // => ƒ external() {}
console.log(obj.external); // => undefined

```

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [December 30, 2021, 5:40am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/2 "2021-12-30T05:40:28Z")

</div>

What's the use case for this? Is it, perhaps, because you want to define a function next to a location where you're using it, like this?

```javascript
const obj = {
  // ...lots of stuff
  function someHelper() { ... },
  a: someHelper(1),
  b: someHelper(2),
  // ...lots more stuff
}

```

Or, do you have some other use case in mind?

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [December 30, 2021, 5:57am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/3 "2021-12-30T05:57:02Z")

</div>

It should be useful when refactoring a large object or array, from which you want to remove some unnecessary properties or items, keeping the code history at the same time.

Take the following snippet as an example:

```javascript
const obj = {
    // ...
    _private: () => {
        // ...
    }, // no need to be exposed in the object
    a : () => obj._private(),
};

obj._private();

```

Without such a sugar, we may do refactor like this:

```javascript
const obj = {
    // ...
};

function _private() {
    // ...
}

Object.assign(obj, {
    a : () => _private(),
});

_private();

```

But with such a sugar, we can directly transform it as a function definition:

```javascript
var obj = {
    // ...
    function _private() {
        // ...
    },
    a : () => _private();
};

_private();

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 30, 2021, 7:18am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/4 "2021-12-30T07:18:58Z")

</div>

What do you mean by "keeping the code history"?

If you want the function to have access to the object, define it inside a closed-over scope that also has access to the object. If you want it not to be exposed, don't expose it.

If you want it associated with an object but not visible by holders of the object, store it in a WeakMap.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [December 30, 2021, 7:39am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/5 "2021-12-30T07:39:12Z")

</div>

"Keeping the code history" means that the shit code has been coded by former contributors and if you want Git to keep tracing the coding history after your refactoring, you should not move the code.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 30, 2021, 7:54am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/6 "2021-12-30T07:54:08Z")

</div>

That's never been a constraint for me when refactoring; when one blames backwards, one often has to stop at refactor commits and restart the blame process from that point.

Either way, the language shouldn't be designed to optimize for the constraints of a version control system, even one as common as git.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [December 30, 2021, 2:43pm UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/7 "2021-12-30T14:43:30Z")

</div>

Putting function declarations in the middle of object literals (and likely, class definitions?) without the expectation that they actually become part of that object/class would make code very confusing.  
I need to read and understand code way more often than looking at its history.

---

<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:** [December 30, 2021, 4:27pm UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/8 "2021-12-30T16:27:32Z")

</div>

+1 for not putting too much weight on 'git blame'. There is a lot more to a function than who was the last author of a particular line. Code gets moved around, files get run through re-formatting tools. If someone is not taking these changes into account when tracking the history then they are likely to arrive at false conclusions.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [December 31, 2021, 1:10am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/9 "2021-12-31T01:10:22Z")

</div>

Not only for 'git blame', but also reducing the complication for reviewers to review code in one "shot" when we can refactor without moving a large snippet of code. Any new thing should bring some overhead for developers to learn, and I don't think a function declaration inside an object or an array will bring us a large barrier to reading code.

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [December 31, 2021, 1:26am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/10 "2021-12-31T01:26:56Z")

</div>

Authoring and reviewing best practices should take care of reducing the impact of reviewing refactors. I see no reason to introduce syntax to the language for such motivation. Any new syntax requires people to learn and understand what that syntax does. When reading your example, it adds more scoping hazards, which are hard to understand, and have been mostly relegated to "legacy" usages of `var` and `function` declarations.

---

<div class="post-metadata">

**Author:** ![aleen42](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aleen42/32/461_2.png) [@aleen42](https://es.discourse.group/u/aleen42)\
**Post date:** [December 31, 2021, 1:30am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/11 "2021-12-31T01:30:53Z")

</div>

I don't think so. Reviewers should also be JavaScript developers, and why can't we motivate a plan for them?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 31, 2021, 1:46am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/12 "2021-12-31T01:46:06Z")

</div>

JavaScript developers have strong expectations informed by 25 years of the language, and this suggestion would violate that.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [December 31, 2021, 7:56am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/13 "2021-12-31T07:56:52Z")

</div>

I think, part of the problem here, is that while you find this particular piece of syntax to be an intuitive addition, others might not. I, for one, without any prior knowledge would expect the proposed syntax to behave the same as inserting the function into an object. e.g.

```javascript
// I would naturally expect this:
const obj = {
  function fn() {}
}
// to be an alternative syntax for this:
const obj = {
  fn() {}
}

```

The fact that the function definition is being hoisted out of the object declaration would be something extra I would need to learn about, and wouldn't be something that I would easily guess, so for me, this syntax addition would have just as high of a barrier of entry as any other syntax addition, and the desire to not want to move code around doesn't feel like a strong enough motivation for new syntax.

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [December 31, 2021, 9:43am UTC](https://es.discourse.group/t/is-there-an-idea-to-add-function-definition-in-an-object-literal-or-array-literal/1140/14 "2021-12-31T09:43:12Z")

</div>

> [@aleen42](#):
>
> Not only for 'git blame', but also reducing the complication for reviewers to review code in one "shot" when we can refactor without moving a large snippet of code.

But in a refactoring such as your example, the body of `_private` is quite important. A reviewer would certainly like to see whether `_private` references `this`. Whether the original `_private` was an arrow or regular method is irrelevant; in either case the new regular function would behave differently.
