Are public and private variables really public or private?
Consider the below example -
|
|
If you try to access the private variable..
|
|
What does private
even mean?
From our earlier definitions, the behaviour seems ok.
Or, is it?
Just compile the above code block to JS.
|
|
And, the compiled Javascript file is -
|
|
There is no mention of variables x
and y
in the class since they are just props. These props of the function can be dynamically defined and created.
There is nothing in the compiled Javascript that will have a problem running the script even if something is defined as private
.
But, private
does prevent successful compilation.
End result: we are pretty much covered with the private variables but be aware of compile and runtime differences in behaviour.
public
can be dropped from the statements
You can as well rewrite the Typescript file as -
|
|
Variables and methods are public by default - you can save on your typing a bit by dropping the public
keyword altogether.