So that was a very basic class.
Now when building classes they often get more complex than that.
And you might want to store more data under let's say a department also should have employees that could
be an array and initially it might be an empty array.
And to also be a bit more precise about what you want to store in there.
It could be a string array to keep it relatively simple.
Now we could have a method here at employee there.
We want an employee as our argument which should just be a string in our example and then we can set
this employees push so push a new employee onto our employees array with this code.
Now we could all add another method print employee in for Mason
and there I went to console log this DOT employees dot length and I also want to console log this DOT
employees itself.
Now if we go down here now we can come it out this code here would which would break again because we
again don't fit the department description with our object down there and instead here on this accounting
department we can add an employee let's say Max and another employee let's say menu and then besides
describing it I also want to get my employee information on there.
If we safeties we should get this output to employees and here's some information.
Now that's all nice to have one problem we have with this class is that we could change employees from
outside not only with an employee but by directly accessing the employees property just as we can do
it for the name as well by the way.
So what I mean is we could also go to accounting and then reach out to employees and maybe add another
employee at index 2 and they're Pozen and I like that if we do that and we save it we get free employees
and we get Anna.
Now you might think well great we got no way.
Well when building more complex applications you typically want a wide something like this don't you
want to make sure that there is one clear path.
One way of using your class and that things like that.
Alternative ways of using it are not really supported because if you're working in a bigger team well
then one colleague is going to use this approach for adding an employee and no colleague might use this
approach and you don't want this you want to have one uniform way of doing this also because maybe in
the ad employee method you do more than just add it to the array maybe you first of all want to have
some validation etc. in here.
So you might have extra code in there which simply does not execute if you directly assign a new value
or add a new value to the array with this line here.
So you don't want to permit that employees is accessible like this from outside of the class.
And typescript Scott you covered you can turn employees here into a private property a private field
by adding the private keyword in front of it.
Now what private means is that employees is now a property which is only accessible from inside the
class.
So from inside the created object.
So any method inside of the department class is able to still work with employees but you can't access
it like this anymore.
Here you see him now getting an error that tried to save.
Does I get a compilation error that the property employees as private and only accessible within the
class department.
So now we forest dead when employees should be added to this department.
People have to use the add employee method because they get an error or if they write code like this.
This is a so-called modifier here.
The private keyword and besides private we all got public which is the default though you don't need
to add public so name like this is exactly the same as name with public in front of it.
The difference is that public properties are accessible from outside.
So for example here I could also reach out to name and assign a new name here if I wanted to.
That is possible for private properties.
This year is not possible we can only use them from inside.
Technically by the way javascript doesn't know public and private only in very modern versions added
recently such a thing exists in the past.
Javascript didn't know private or public properties all properties always were public there typescript
introduces this feature which of course does not work during runtime however because javascript.
Until recently only new public properties.
So this line here frozen area during typescript compilation.
If you compile it to JavaScript nonetheless this code would still executed runtime without errors because
again javascript on its own until recently didn't notice concept.
So depending on the where's your compiling for it still won't know it and therefore typescript only
supports this because it checks it during compilation not at runtime with it.
However we can a wide code like this and force everyone to write cleaner code.