In the section we're gonna start to discuss classes in typescript.
As usual we'll get a plain definition some examples and then move on to discussing where we're going
to actually use classes.
This is the last big subject we're going to discuss before we start working on some interesting applications.
So let's get to it.
All right so first off definition so a class is a blueprint of sorts.
We are writing code here but it's a blueprint print or a model of sorts.
We're going to use to create an object that has some different fields and methods attached to it.
We use these classes to represent different things or objects inside of our application.
Now whenever I say fields I'm referring to different values that are going to be attached to an object.
And whenever I say methods where return referring to different functions they'll be attached to an object.
Now we're going to break covering classes into kind of two separate subjects.
We're going to first discuss how classes work with methods or functions and then come back and add in
the idea of fields and values to them.
So let's get to it.
It's going to flip on over my code editor in inside my project directory.
I'm gonna make a new file called it classes dot yes then inside of here I wanted to find a new class
that's going to represent a vehicle very similar to what we did in the last little example we went through.
So I'm going to write out a new class definition by using the class keyword and then the name of the
class.
We're always going to capitalize the name of a class then inside of this class body we can define different
methods.
So I might write out a method called Drive all annotated as taking no arguments and maybe a return type
of void in inside of here maybe else do a quick console log and say something like chug it sugar when
we create a class we are creating a blueprint or a definition of what a vehicle should be.
We don't actually call the methods attach this class directly.
In some cases we can but with normal methods we do not.
Instead we're going to use a class to create an instance of a class to do so we're going to use the
new keyword.
So for example down here we might say const vehicle is new vehicle like so notice a capitalization that
we're using here.
Traditionally we're going to name all of our different classes with a capital letter and then anytime
we have an instance of that class well you will sometimes use the same word but spell it out with lowercase
like so.
So now with this instance of the class we have access to all the different methods we define inside
there.
So for example we could do something like vehicle dot drive like so now we've not been running a lot
of the different code examples we've put together but with classes it's really important to actually
execute some code so you get a really good idea of what's going on.
So I got to very quickly save this file and then flip back over to my terminal to run it back inside
my project directory remember we can run some code with T.S. dash node and then the name of the file
in this case classes dot t yes you know I can see the council log up you're right there we can attach
as many methods as we want to to a class so for example this class might also have a method called hunk
it's not going to return anything in a time I call it maybe we will do a console log of beep like so
so we can save this file flip back over once again oh one thing I forgot here sorry.
Down here at the bottom we want to make sure we also call that method we just added so we'll do a quick
vehicle dot honk like so now we'll say the file flip back over and then run that file again and now
we can see that beep appear now I'm going to sue if you're in this course you've probably seen classes
before so I just want to make sure you got a quick reminder some the basics around them.
Let's take a quick pause right here when we come back the next video we're going to start to discuss
how classes in typescript are a little bit different than classes in a traditional like yes 2015 javascript
so quick pause and I'll see you in just a minute.