And now our nice feature related to properties and methods which exists also in JavaScript when you're
using iOS 6 or later but also typescript are static properties and methods static properties and methods
allow you to add properties and methods to classes which are not accessed on an instance of the class
so where you don't need to call new class name first but once you access directly on the class this
is often used for utility functions that you want to group or map to a class logically or global constants
which you also want to store in a class.
An example built into javascript which is not defined by TypeScript and not defined by you but part
of JavaScript in the browser is the math constructor function or a class if you want to call it like
this that's globally available in JavaScript where you can access pi as a constant value to give you
that pi number or functions or methods to be precise like p o w to calculate the power of something.
These are methods on properties which you don't access on the instance of math you don't have to call
new math first.
Indeed that won't work but you access these properties and methods directly on the class itself.
So moth acts more like a namespace as a grouping mechanism here and that's a common use case for static
methods and properties and you can add them to your own classes as well let's say on department we want
to have the method that helps us create employees and dad is something we might want to be able to access
without instantiating department because we typically instantiate our more specialized versions I.T.
department and accounting department and we don't really want to instantiate department just to call
a utility method and therefore we can Adam effort here create employee let's say where we want to get
the name which is a string as an argument and there we want to return something now to make that a static
method which we can access without instantiating this class we add the static keyword in front of this
method and then in here we maybe return an object where we have the Name property mapped to this name
value.
Now of course that's a very simple method but you get the idea now to use that we could go down there
where we execute our code and now let's say we want a new employee employee 1 well then we can just
get this with the part meant dot create employee and pass and Max for example and if I then console
log employee 1 we should see an object with a name of Max.
So if I save that indeed here there's this deep employee we just created.
So this would be a typical example for a static method we call it directly on the class without the
new keyword and therefore we use the class as a grouping mechanism you could say and if you would want
to add a static property you could do that as well if you had something like the fiscal year here that
you currently want to use in your company.
You could set this to let's say 2020.
And then again to make it available without instantiating this you could add static in front of this
property or in front of that field and now X is dead.
So now down there.
Right console log my created employee here.
We can also access Department DOD fiscal year.
Just like that without instantiating it.
And if we save that unsurprisingly we'll see 2020 being locked here.
So these are static methods and properties.
One important word about them or one thing you should keep in mind when you add them on a class you
can't access them from inside your non static parts.
So if you feel like accessing the fiscal year in the clear in the constructor and you want a console
log it here this as you can see will not work.
I'm getting an error here that the property fiscal year is a static member we can't access this here
because the constructor and basically anything in there all the methods as well which are not marked
with static and you can't mark de constructor s static by the way will not be able to access static
properties because this does refer to the instance created based on the class.
Well the static property is not a way to block the instance because the whole idea behind static properties
and static methods is that they're detached from instances.
So of course you can't access them with the this keyword if you would want to use the static property
or a method from inside the class.
You would have to use to class name here to access it.
This gives you access to static properties and methods.
Also from inside the CLOs that's one important thing to keep in mind.