In this short video,
let's quickly gonna talk about statics methods.
Now a good example to understand
what a static method actually is,
is the build in Array.from method.
And let's just do that here in the console.
so remember that we have the Array.from method
which converts any array like structure to a real Array.
So for example document.query selector.
All right.
So that's an error right there and so you see,
this now return an Array to us.
And for some reason it is empty.
And that's because query selector
doesn't return any note list.
So lets try query selector all,
and so now indeed we get an Array here.
But that's not really the point,
what's the point is that this from method here
is really a method that is attached
to the Array constructor.
So we could not use the from method on an Array.
So this is not gonna work.
All right.
So from is not a function.
And so that is because this from method here
is really attached to the entire constructor,
so the Array constructor
and not to the prototype property of the constructor.
All right.
And so therefore all the Arrays do not inherit this method.
Again because its not on their prototype.
Its simply attached to the constructor itself.
So Array.from here is basically just a simple function,
but its a function that's attached to the Array constructor.
And the reason for that is simply,
so that developers know that it is related to Arrays.
We also say that the from method is in the Array name space.
And we actually used that term before for some methods
in the number and in the internationalization name space.
Remember for example numbers.parsefloat.
So that's the same thing.
Okay.
So this method is another static method
and its static on the number constructor.
So its not available on numbers,
but only on this very constructor.
All right.
So these are some good examples that we understand
what a static method is.
And we usually use these kind of as helpers,
that should be related to a certain constructor.
And so maybe you can imagine that it should be pretty easy
to implement static methods or selfs.
And let's do that for both or constructor function
and also for the class.
So here I already have the constructor function
that we wrote right in the beginning of the section.
So this person here and to add a static method,
all we have to do is write,
person and let's say I want to create a very simple
hey function
okay.
So which simply like waves when we call it.
So lets just say console.log,
hey there.
And then maybe some emojis so we can actually
see it better in the console.
And so now in order to call this, it is pretty easy.
We just have to do
person.hey
and that's it.
So somewhere up here we now should get this hey there.
Great but of course this one is not inherited.
So just like we cannot call the from method on an Array.
we cannot say Jonas.hey
because it is simply not in the prototype of
the Jonas object.
So there's noway that the Jonas object could inherit it.
All right.
Now just let's just take a look at the disc key word,
but this again should be pretty obvious.
And so that's essentially the entire
constructor function here.
And the reason for that is because,
that is exactly the object that is calling the method.
And so as always that is basically the rule.
So whatever object is calling the method,
will be the disc key word inside of that function.
And so here the disc key word,
is simply that entire constructor function.
Great.
And now let's quickly take this do the same in class,
where it is even easier.
So here to create a static method,
all we need to do is to add the static keyword.
So static hey
and then that's it.
So let's just write it here so that is a static method.
While these ones is called instance method.
So as it says here these are methods
that will be added to the prototype property,
so that all instances can have access to them.
And therefore the name instance methods,
okay.
And now we can simply, let's do it here at the end.
Can simply do personcl.hey.
And so again we get hey there and this time,
disc key word points to the entire class.
All right.
So keep in mind that these static methods
are not available on the instances,
and sometimes they are still useful to implement
some kind of helper function about a class
or about a constructor function.