Public and private class fields
11–20 of 112 posts
Re: Public and private class fields
#12Was this # thing voted by the community or something?
Re: Public and private class fields
#13I 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?
Re: Public and private class fields
#14This 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.
Re: Public and private class fields
#15I 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.
Re: Public and private class fields
#16This 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
#17This 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.
Re: Public and private class fields
#18Why not use the private/public keywords like in TypeScript? Was this # thing voted by the community or something?
- 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
#19Also, will there be a way to do protected?
Re: Public and private class fields
#20Wow, the '#' prefix is so ugly.
Agree. `public` and `private` keywords would be much better.