Now,
we added our first class here and at the moment,
it's already useful to allow us to
create multiple objects with different properties.
But as I mentioned before,
you can always add logic to your own classes.
You can add your own methods
just as to build in date
blueprint has this
to ISO string method,
and you can add your own methods
by simply adding them inside of this
class
block.
So not inside of the constructor,
you only use the constructor to
initialize your object
and to add the properties with which it should start.
But methods on the other hand are added
to the class like that after the constructor.
For example here,
we could add a describe
method
where I wanna,
output some information about the job
and here we could use that
template literal feature we learned about
and say,
I'm
a,
and then add the title
and we'll do that in a second.
Then we add a comma and say,
I work in
and have another placeholder for the location.
And then I maybe say,
and I earn,
and we have a last placeholder for the salary.
Now the question just is
how do we fill those placeholders?
Well in the end here,
I want to refer to the title.
Here I want to refer to the location
and here in the last place holder,
I want to refer to the salary.
Now these are all properties in this object,
but how can we now refer to these properties
from inside a method
that belongs to the same object?
We can't just use the property name like that.
If I just type title here,
I'm not getting an error,
but actually title won't be defined
in this function,
in this method,
because it's not a constant
or variable that would be globally available
in this block or in a surrounding block.
Instead,
title here really is just a property of this object.
Now we also can't say job dot Title
because the job is also,
not a variable that doesn't exist here.
And maybe with a capital J,
then it would be a class,
but the class is just a blueprint.
Here I need the concrete title
of an object based on that
blueprint.
So that's also not the solution.
Instead it's again the this keyword
in the constructor,
we use the this key word to refer to the object
That's about to be created.
In all the other methods of this blueprint,
We'd refer to the already
created
object.
So this here,
again refers to the object that
was
created,
actually that this keyword can be
a bit tricky in JavaScript,
but in general, it works as I just described it.
It allows you to refer to the object
on which you call this method.
So therefore,
here we can then output this
location.
And then here at the end,
we output this dot salary.
And now with that,
we're using the this keyword to refer to
the different properties inside of this method,
which also belongs to the blueprint.
And since we defined this method in the blueprint,
we can use it on
all the objects we create based
on the blueprint.
So instead of console logging my developer like this,
I can write developer,
describe,
and execute this method.
And do the same for the cook and call describe.
And that again,
shows us why blueprints are useful if we create
multiple objects of the same type,
we only have to define the properties once.
Now,
we also only have to define our logic
that belongs to this object once
and still,
we can then use this method on
multiple different objects
that were created based on the same blueprint.
and there for,
Of course, if I now execute this,
I get the expected output where
I have different values for title,
place,
and salary for the different objects.
So
classes are another feature that can be useful.
Sometimes you create your own classes
so that you have your own reusable blueprints.
But very often you will also work with
built in
classes,
Built in blueprints,
that are either built into JavaScript itself,
or that are provided by third-party packages.
As all the concepts I explain in this module,
we'll also see the new keyword
and classes again later in the course.
And it's now hopefully clear
what the class keyword does
and what the new keyword does
and how the two concepts work together
to allow
you to create
and use blueprints
to then build objects
based on these blueprints.