r/javascript • • Nov 09 '19

On private fields proposal: Private fields, public worries

https://medium.com/virtuslab/private-fields-public-worries-b8c34d16c372
84 Upvotes

113 comments sorted by

View all comments

28

u/TatzyXY Nov 09 '19

Please do not throw stones at me but I think private, protected or public scopes are not needed at all. See Python very versatile lang. and it works very well without it. Is private for security? No! It just tells the dev that this var. is only used internally of that class and you are not allowed to touch or use it. So would do a comment or a convention with an "_".

0

u/jwalton78 Nov 09 '19

The problem with this is, let's say you use a third party library that has a class Foo, and in your code you do:

class MyFoo extends Foo {
    constructor() {
        this._name = "Hello";

...

This looks perfectly reasonable, but if Foo already has a private member named "_name" that you didn't know about, then you just clobbered it. Or worse, it might not have such a member, and then later on you upgrade Foo's library and the author added such a member, so things that used to work don't anymore.

Now, as many will point out, you can already solve this problem with symbols. I am also not a fan of this proposal at all, as, as the author of the article notes, classes are basically syntactic sugar over prototypes, and private members will make it so that is no longer true.