# Function object initializer

**URL:** <https://es.discourse.group/t/function-object-initializer/1886>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [January 1, 2024, 6:19pm UTC](https://es.discourse.group/t/function-object-initializer/1886 "2024-01-01T18:19:37Z")\
**Posts on this page:** 1\
**Showing post:** 15

<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:** [November 20, 2024, 9:22am UTC](https://es.discourse.group/t/function-object-initializer/1886/15 "2024-11-20T09:22:00Z")

</div>

I'm warming up to the idea of a shorthand object-function declaration. Besides allowing to declaratively create functions that have own properties, it would make it simpler to create unbound functions that have no construction behavior.

First some recap on the 3 kinds of function declarations that exist in the language today:

- `function() {}` creates a function sensitive to a `this` arg that can also be called as a constructor
- `() => {}` aka arrow functions create a function with a bound `this` arg that cannot be called as a constructor
- `foo() {}` short hand methods (in object literals or class definitions) create a function that is `this` arg sensitive but cannot be constructed.

The only way to create the latter kind of function today is to use a temporary object literal and extract its short hand method.

Short hand object-functions, `{ () {} }`, would simplify this, while enabling custom prototypes.

```javascript
const foo = {
  (bar, baz = null) { ... },
  __proto__ : CustomFunction.prototype,
  expando: 42,
};

```

Would be equivalent to:

```javascript
const foo = Object.setPrototypeOf(
  Object.defineProperties(
    { foo(bar, baz = null) { ... } }.foo,
    Object.getOwnPropertyDescriptors({ expando: 42 }),
  ),
  CustomFunction.prototype,
);

```

---

_[View the full topic](https://es.discourse.group/t/function-object-initializer/1886)._
