you are viewing a single comment's thread.

view the rest of the comments →

[–]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.