# Shorthand To Initialize Private Class Property

**URL:** <https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866>\
**Category:** 💡 Ideas\
**Created:** [July 17, 2021, 9:13am UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866 "2021-07-17T09:13:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethanlal04](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ethanlal04/32/918_2.png) [@ethanlal04](https://es.discourse.group/u/ethanlal04)\
**Post date:** [July 17, 2021, 9:13am UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/1 "2021-07-17T09:13:30Z")

</div>

Let's say we have a class called `Person`. `Person` has an `age` property that is private and is initialized through the `constructor`.

Like this:

```javascript
class Person {
  #age;

  constructor(age) {
    this.#age = age;
  }
}

```

We have a lot of repetitions of `age` just to do something simple. I guess it'd be good to initialize it in the parameter area as we do in Typescript.

Like this:

```javascript
class Person {
  constructor(#age) {}
}

```

We could also keep default properties there.

```javascript
class Person {
  constructor(#age = 18) {}
}

```

Thank you 😀.

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [July 17, 2021, 4:39pm UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/2 "2021-07-17T16:39:19Z")

</div>

Yay! Our very old parameter properties. This is a much wanted feature. Maybe it'll happen someday. Also see: [this](https://es.discourse.group/t/parameter-properties-in-js/683)

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [July 19, 2021, 2:16am UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/3 "2021-07-19T02:16:02Z")

</div>

Of course, if you abandon that OOP stuff and defect to the Functional side, we have all the good syntax:

```javascript
const Person = ( age = 18 ) => ({});

```

I mean, look how beautiful this is:

```javascript
const Person = ( age = 18 ) => ({

    announce: () => console.log( `I am ${age} years old.` ),
    grow: () => age ++,
    ascend: () => age = Infinity,

})

const centenarian = Person( 100 );

```

But, I think JS should support every programming paradigm, and decorators can't help here.  
So, it's still a good idea. 👍

---

<div class="post-metadata">

**Author:** ![ethanlal04](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ethanlal04/32/918_2.png) [@ethanlal04](https://es.discourse.group/u/ethanlal04)\
**Post date:** [July 19, 2021, 1:52pm UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/4 "2021-07-19T13:52:02Z")

</div>

Hello @0X-JonMichaelGalindo,

> [@0X-JonMichaelGalindo](#):
>
> Of course, if you abandon that OOP stuff and defect to the Functional side, we have all the good syntax:

I don't agree with that. In coding, every problem has a unique solution. You can't use the same technique to solve all your problems. This is kind of like "If all you have is a hammer everything looks like a nail".

But thanks for agreeing :)

---

<div class="post-metadata">

**Author:** ![0X-JonMichaelGalindo](https://avatars.discourse-cdn.com/v4/letter/0/4af34b/32.png) [@0X-JonMichaelGalindo](https://es.discourse.group/u/0X-JonMichaelGalindo)\
**Post date:** [July 19, 2021, 2:22pm UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/5 "2021-07-19T14:22:04Z")

</div>

> [@ethanlal04](#):
>
> You can't use the same technique to solve all your problems

Obviously, I can't solve all my problems with real functional programming (immutability, no side effects). I just wish I could because in JS it has really beautiful syntax. That's all I meant.

OOP with classes in JavaScript (like most languages) has a boilerplate problem. Your suggestion helps.

---

<div class="post-metadata">

**Author:** ![ethanlal04](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ethanlal04/32/918_2.png) [@ethanlal04](https://es.discourse.group/u/ethanlal04)\
**Post date:** [July 19, 2021, 3:35pm UTC](https://es.discourse.group/t/shorthand-to-initialize-private-class-property/866/6 "2021-07-19T15:35:32Z")

</div>

Sorry if I sounded rude. I was clarifying your statement for future readers because it sounded a little bit like what I said :)
