Typescript: Difference between declaring class variables using private, public and nothing

What's the difference between:

A.
class foo {
  bar: string;
}

B.
class foo {
  private bar: string;
}

C.
class foo {
  public bar: string;
}

Apparently, I managed to access the bar in all three cases using the following:

var temp = new foo();
temp.bar = 'abc';
+4
source share
1 answer

bar: string100% equivalent public bar: string. The default availability modifier is public.

private- only confidentiality during compilation; there is no execution at runtime, and the emitted code is identical regardless of the access modifier. You will see an error message from TypeScript when you try to access a property outside the class.

protected, private, , . , JavaScript.

+4

Source: https://habr.com/ru/post/1650364/


All Articles