About the implementation of class features...

help

Does anyone know when we will get a decent way to create public, private and protected class members ? I come from c++ and js way to do those irks me.

I can't fathom the reason behind the half-assed implementation... Did they want to taunt and torture the oop people ? /rant

Posted Dec 15, 2016, 3:49 AM UTC

013
rauschma•

The design motto for ES6 classes was “maximally minimal”, for two reasons: To ensure no mistakes were made. And to get consensus in favor of this feature.

For now, I either use symbols as keys for private properties. Or I prefix property names with underscores (relying purely on a naming convention without actually protecting them in any way).

Improvements take time, a proposal for private fields is in the works: https://github.com/tc39/proposal-private-fields

2
MetaParserOP•

I've been using var declaration and getter/setter in the constructor. I've have a class that have a custom creator that automatically make the variable with the visibility required in the constructor. All you have to do is inherit from it and send the var in the proper format to the super().

I had to figure how to do multiple inheritance with Class too that was fun, :X.

[edit] why do they need their funky symbols. the keyword private is the obvious choice there, why an unclear #...

1
Bakkot•

why do they need their funky symbols

There's an FAQ which is mostly about exactly that question.

1
MetaParserOP•

You're a funny one. A lot of language manage to do great without it. Js syntax can become shit very quickly, and not even talking about the jQuery...

1
Bakkot•

A lot of language manage to do great without it.

Other languages do not have JavaScript's object model.

1
[deleted]•

[deleted]

1
azium•

Imagine asking this same question replacing JS with Haskell. Certainly it's not a fair comparison, but the fallacy is the same. Different languages have different strengths and patterns that make them good/popular/useful. brainfuck is a language that taunts and tortures.

1
MetaParserOP•

Haha, yeah the brainfuck. I use JS because I have to, I wouldn't if I had the choice.

1
azium•

Aw really? I <3 JavaScript. It's got some of the best language features out there (rest/spread operator, lambdas, async/await, optional typing, template tags and so much more!)

1
MetaParserOP•

I might get to love it later, but I have been on it for 1 year, and I'm still missing the cpp. I hope they implement the shared object for worker soon, it would be cool to have a worker than can access the document too...

1
inu-no-policemen•

For the time being, use leading underscores for wannabe privacy. Once there is actual privacy (if ever), you can update your code.

While leading underscores are only a convention, they do mark things as private. You are free to change/rename those things without bumping the major version (semver). If you break someone else's code, it's their own fault. They knew very well that they shouldn't have touched any of the internal stuff.

You can also emulate privacy by sticking things in a WeakMap, but it requires some boilerplate and is kinda annoying to use. I don't recommend it.

Another option is TypeScript. It supports private/protected access modifiers.

0
MetaParserOP•

I've been using var declaration and getter/setter in the constructor. I really need that private feature. I've heard of TypeScript, will check it at some point thanks.

1
inu-no-policemen•

In case you're curious, the privacy via WeakMap workaround looks like this:

https://jsfiddle.net/rzgumjL4/

1
[deleted]•

[deleted]

1
MetaParserOP•

I ll look at it, at the first look, might be a better alternative to my own custom stuffs with a bit of work, thx.

1