Live data from Hacker News

Public and private class fields

developers.google.com

11–20 of 112 posts

Re: Public and private class fields

#13
post #8

I don't understand why public fields need the underscore prefix. Aren't all fields public by default? Why do you need to add a prefix to "hide" public fields?

I think the idea is to distinguish them from class getter functions? I don't really understand it either.

Re: Public and private class fields

#14
post #5

This is Exhibit N for the "the class keyword is ruining Javascript" prosecution. If you want a "private variable" you just use a closure. let incrementer = () => { let x = 0; return { value: () => x, increment: () => x++ }; }; It's like the OO people are determined to forget about the functional concepts that made JS great.

What are some other examples of the class keyword ruining Javascript?

Re: Public and private class fields

#15
post #10
post #8

I don't understand why public fields need the underscore prefix. Aren't all fields public by default? Why do you need to add a prefix to "hide" public fields?

They don't, that's just the example they chose. Prefixing variables / functions with the underscore has long been a signal for "this is private" in languages that don't provide the functionality.

This is true, but the whole point of this article is to introduce a new way of handling private class members.

Re: Public and private class fields

#16
post #14
post #5

This is Exhibit N for the "the class keyword is ruining Javascript" prosecution. If you want a "private variable" you just use a closure. let incrementer = () => { let x = 0; return { value: () => x, increment: () => x++ }; }; It's like the OO people are determined to forget about the functional concepts that made JS great.

What are some other examples of the class keyword ruining Javascript?

Decorators, which have to be added as a language concept because of classes, when otherwise higher-order functions would do what needs to be done.

Re: Public and private class fields

#17
post #5

This is Exhibit N for the "the class keyword is ruining Javascript" prosecution. If you want a "private variable" you just use a closure. let incrementer = () => { let x = 0; return { value: () => x, increment: () => x++ }; }; It's like the OO people are determined to forget about the functional concepts that made JS great.

and now you are defining you whole class inside that return {}. Do you honestly find this more readable?

Re: Public and private class fields

#18
post #12

Why not use the private/public keywords like in TypeScript? Was this # thing voted by the community or something?

The justification is that they needed a unique accessor, because:

- attempting to access a non-existent private property using `this.prop` would create a public property "prop" in JS, which they claim would be a source of bugs.

- they also wanted to allow creating a public property on a subclass with the same name as a private property on a parent class—having the accessors the same could be confusing.

That said, they could still have had keywords for definition with an alternative accessor. # is definitely not idiomatic; it's extremely ugly.

Re: Public and private class fields

#20
post #4
post #2

Wow, the '#' prefix is so ugly.

Agree. `public` and `private` keywords would be much better.

public and private are already reserved keywords in JavaScript strict mode, so there should be no issue with web compatibility.

https://mathiasbynens.be/notes/reserved-keywords

Post reply on HN