In the section, we're going to 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 blue print or a model of sorts that 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.
And whenever I say fields, I'm referring to different values that are going to be attached to an object.
And whenever I say methods, we're referring to different functions that will 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.
I'm going to flip on over to my code editor in inside my project directory.
I'm going to make a new file called Classes Dots.
And inside of here, I want to define 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, I'll annotate it as taking no arguments and maybe a return type of void.
And inside of here, maybe I'll do a quick consultation.
Say something like chug, chug a.
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 attached 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 the capitalization that we're using here.
Traditionally, we're going to name all of our different classes with a capital letter, and then any
time we have an instance of that class, we 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 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.
It's going to very quickly save this file and then flip back over my journal to run it.
Back inside my project directory, remember, we can run some code with TS Download and then the name
of the file in this case classes Dotty's.
Now, I can see the council log up 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 honk.
It's not going to return anything.
And any time I call it, maybe we will do a log of beep like so.
So we can say this file flip back over once again.
OK, 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 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 it classes before, so I just
want to make sure you got a quick reminder, some of the basics around them.
So 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 E 2015 JavaScript.
So quick pause and I'll see you in just a minute.