# The final keyword for classes

**URL:** <https://es.discourse.group/t/the-final-keyword-for-classes/2351>\
**Category:** 🦋 Proposals\
**Tags:** proposal\
**Created:** [April 17, 2025, 7:58am UTC](https://es.discourse.group/t/the-final-keyword-for-classes/2351 "2025-04-17T07:58:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nihil-pro](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nihil-pro/32/2412_2.png) [@nihil-pro](https://es.discourse.group/u/nihil-pro)\
**Post date:** [April 17, 2025, 7:58am UTC](https://es.discourse.group/t/the-final-keyword-for-classes/2351/1 "2025-04-17T07:58:41Z")

</div>

In order to protect certain public properties in a class, we need to use the Object.defineProperty method with a configurable and writable value set to false. While this is not very convenient, we would like to have a keyword that could be used for this purpose. For example, the _final_ keyword.  
It may also be a good signal to the JIT compiler that this property or method will not be overwritten or changed.

```javascript
class Foo {
  final bar = 'baz'
  final static bar = 'baz'
  final method() {}
}

```

We also sometimes use the freeze() method for both protection and optimization purposes, which can be inconvenient for classes. The final keyword could also help us in this situation.

```javascript
final class Foo {}
// just sugar for
class Foo {
  constructor() {
    return Object.freeze(this)
  }
}

```

Thus, the keyword is eventually introduced as syntactic sugar for Object.freeze(obj) and Object.defineProperty(obj, prop, { writable: false, configurable: false })

---

<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:** [April 17, 2025, 11:36am UTC](https://es.discourse.group/t/the-final-keyword-for-classes/2351/2 "2025-04-17T11:36:49Z")

</div>

Related: [Final classes](https://es.discourse.group/t/final-classes/1621)

---

<div class="post-metadata">

**Author:** ![blob](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/blob/32/2230_2.png) [@blob](https://es.discourse.group/u/blob)\
**Post date:** [May 4, 2025, 9:20pm UTC](https://es.discourse.group/t/the-final-keyword-for-classes/2351/3 "2025-05-04T21:20:53Z")

</div>

`final static` and `final method()` makes sense, not sure about properties though, since those are normally assigned in the constructor. If you're looking for a constant value then putting this on the prototype makes more sense and I'm all for a nice syntax for that, although maybe not `final`
