proposal-class-fields icon indicating copy to clipboard operation
proposal-class-fields copied to clipboard

Please Don't Do That.

Open luciedefraiteur opened this issue 4 years ago • 8 comments

I really often need to access private fields in librairies i use in order to fix small issues, as a workaround, cause the library is badly done, really really often, or just to debug quickly something, would be really boring and against whats good in javascript, to force people to respect private members.

Please do not add this feature.

luciedefraiteur avatar Jun 05 '21 04:06 luciedefraiteur

It’s already stage 4. It’s added.

If you want to change private fields, change the code.

ljharb avatar Jun 05 '21 07:06 ljharb

what's the motivation for this change? whoever though of this never used js that much, one thing that makes js nice is the ability to access and change objects, this seems a poor choice, with some luck linters will discourage it as bad practice, damage is done though

gr1m1um avatar Jul 28 '21 06:07 gr1m1um

The most important feature in JS is to use lexical scoping to prevent access to things. The ability to access things by default is not something that makes the language nice.

ljharb avatar Jul 28 '21 06:07 ljharb

makes some sense but in practice its often handy to change and check things, being a plain language and malleable I think its a big plus of js, I can't find the motivation for this proposal written down or think its reasonable

gr1m1um avatar Jul 28 '21 06:07 gr1m1um

okay there is some explanation on the docs, makes sense to separate public from private api also many times ran into this or wish private fields could be more explicit, still considering the downsides don't think this is an improvement

gr1m1um avatar Jul 28 '21 06:07 gr1m1um

@jbreckmckye no, it does not make it moot at all. The concern is about changing things at runtime. It’s never been and never will be possible to prevent changing the code at compile time.

ljharb avatar Sep 14 '21 15:09 ljharb

The fact that a library author can add private fields to a class used as implementation detail and unknowingly break all consumers is quite a big downfall of the current private fields.

trusktr avatar Dec 25 '21 21:12 trusktr

Assuming a library author is willing to give up any semblance of the ergonomics that TC39 thought was so very important, it's possible to actually use private fields in a library in a way that is completely non-disruptive to downstream users. However, the fact that this kind of non-ergonomic coding has to be used just to fully ensure that my use of private fields doesn't disrupt how others want to use my code only highlights how many unfortunate conflicts of interest went into this proposal.

Oh well. It's not like they're going to fix it at this point. The damage is already done. No real use in complaining about it. We can either work around it, or opt to not use it. That's all we've got.

rdking avatar Jan 08 '22 02:01 rdking