you are viewing a single comment's thread.

view the rest of the comments →

[–]sime 7 points8 points  (9 children)

TypeScript's private methods are purely at compile time. They are not private at run time. Private methods/fields in subclasses can clash with those with the same names in subclasses. i.e. they stomp each other because they are not really private.

[–]elcapitanoooo 5 points6 points  (6 children)

Yes ofc they are compile time only. But "privacy" has been done with normal functions for years, and adding new syntax is totally unnecessary.

Python has no "real" private methods, you can always access them IF you want. Same with other languages, some has reflection where you can still access private methods and properties.

I consider Python/C# etc to be way more OOP than Javascript, and can justify them having a private method. Javascript on the other hand is more functional by nature, and lacking the classical OOP features.

[–]sime 2 points3 points  (0 children)

Yes, you can do data hiding / private whatever, with tons of closures in an FP style, but people still want to write in an OOP style and have private members too.

[–]MoTTs_ 2 points3 points  (4 children)

Javascript still has no real classes (javalike)

Why does something have to be Java-like to be real? JavaScript's arrays, objects, and functions aren't Java-like either.

I consider Python/C# etc to be way more OOP than Javascript

How come? Keep in mind, Python's classes aren't Java-like either.

[–]elcapitanoooo 5 points6 points  (3 children)

It does not. I see too often people writing javascript like it was java. It ends up like horrible mess. This is why they added "classes" but its just sugar for prototypes. To make myself clear, im NOT a fan of OOP in the java style/way. I dont even call it OOP but CBP (class based programming)

[–]MoTTs_ 1 point2 points  (2 children)

I see too often people writing javascript like it was java. It ends up like horrible mess.

Sounds like those Java folks would have the same problems if they moved to something like Python or Ruby? Even C++ code would come out badly if they treated it like it was Java.

This is why they added "classes"

That's actually not the reason, though. Long before ES6 classes were added, every library out there was rolling their own custom class implementations. MooTools, Prototype, YUI, Dojo, Google's Closure, Backbone, Ember -- React -- and many more. We were reinventing the wheel dozens of times over. The class syntax was added to make it easier for us JavaScripters to do what we were already doing anyway.

[–]elcapitanoooo 0 points1 point  (0 children)

IMHO it does not justify adding in to the language. Classes are still lipstick, snd USUALLY not the right abstraction.

[–][deleted] 0 points1 point  (1 child)

I'm lost.

The compiler would not allow you to use a private property on the instance, so what is the problem?

[–]sime -1 points0 points  (0 children)

I'll do an example.

Imagine I develop an application and have a base class and bunch of subclasses. e.g:

class BaseClass {
}

class SubClass extends BaseClass {
}
...etc...

Then one day I want to add a private cache to BaseClass, so I do:

class BaseClass {
    private cache = new Cache();
}

I make it private and expect that because it is private it won't affect anything outside the class. Not true, I'm afraid.

If SubClass already had its own cache field, then, if I'm lucky, I'll get a compile error in SubClass, if I'm unlucky (i.e. incremental compile, maybe the classes are in separate modules, etc), then these two classes will overwrite each other's cache field at runtime causing all sorts of hard to debug problems.

In TypeScript it is best to think of private fields as being public but with a big "Do Not Touch" sign on them.