Hacker News new | ask | show | jobs
by rickreynoldssf 978 days ago
There are private properties in classes

class Foo { #ImPrivate = true; #meToo() { if(this.#ImPrivate) cantTouchThis(); } }

new Foo().#meToo() === BOOM

Also with using closures for "classes" takes up a whole lot more memory because new instances off all the closures are created for each instance of the class.

2 comments

There are also getter (and setter) properties that can be backed by private fields:

get name() { return this.#name; }

Half of this article seems to be based on the author’s unawareness of JS class syntax.

The optimization points are good to keep in mind, no?

See for example:

https://github.com/rollup/rollup/issues/349

I think it is good to know that there is no tree shaking for class methods, even when public.

This is a valid consideration IMO.

The missing minification of identifiers and properties of the Vue instance in general were always bugging me in Vue 2, even when not using the class keyword.

Private properties don't work with Proxy.

https://lea.verou.me/blog/2023/04/private-fields-considered-...

Quote:

> Private fields, proxies, pick one, you can’t have both.

For the same reason, they are forbidden in MobX. Hello underscore!

And thank god for TypeScript compiler's private keyword! While... I can see how this plethora of possibilities looks confusing.