Hey, guys.
Good to see you back.
This is the second tutorial related to object oriented programming.
And as you can see side by side, I opened my Visual Studio Code editor and my browser using lib server
extension, and I already created an HTML document name index dot HTML.
So inside the script tag first I'm going to create a class.
To create a class in JavaScript we need to use class keyword class.
Then we need to set the class name and our class name is hello.
Then inside the curly braces here I'm going to pass a message.
Basically I'm going to define a method named message.
So I'm going to type message round braces.
Then inside the curly braces here I'm going to print console dot log inside the round braces.
Inside the double quotes I'm going to print a message.
Hello everyone.
And then semicolon to in this line.
So whenever user call this method from this class then it's going to print this message.
Hello everyone.
And as I told you to call this method from this class we create an object using this class.
So let's create an object using this class let and our variable name is a a equal to.
To create an object we need to use new keyword new.
Then we need to call the class name.
Hello.
Now a variable becomes an object which is made with hello class.
And now I want to call message method.
For that we need to type a dot message and semicolon too.
In this line if I save this file, as you can see in my console, it print.
Hello everyone.
Now you understand.
First we need to create a class.
Then inside this class we have a method named message.
If we want to call this method then first you need to create an object using this class.
In our case A.
Then you can call this method from this class.
Now let's talk about more about methods in JavaScript we have total three types of methods.
Our first method is constructor method, second one is prototype method and third one is static method.
And we are going to start with constructor method.
To create an constructor method, we don't need to take any constructor name.
Let me show you how.
So here I'm going to create a constructor method.
For that just we need to type only construct our constructor round braces.
And inside the curly braces you can pass your message.
Suppose here I'm going to type console dot log inside the round braces I'm going to type welcome.
And semicolon too in this line.
And remember to create and constructor method.
You don't need to take any name just you need to type constructor keyword.
Now the question is what is the purpose of this method.
Whenever we create an object using this class, then constructor method automatically called.
We don't need to call it by ourselves, it automatically run.
Now if I comment out this line and save this file again, now you can see it automatically run the constructor
method.
So it's print welcome here.
Just create an object using this class.
But I do not call the particular any method.
Similarly if I create another object using the same class.
So I'm going to duplicate this line.
And here I'm going to type B and save this file.
After save this file as you can see again it print welcome in our console.
First it run for a object then it run for B object.
And now we are going to learn about properties.
And also we are going to learn how we can assign value in properties.
There is two option that we can do it.
First I'm going to show you a basic method.
So I'm going to remove this line.
Listen carefully.
Whenever we need to declare an property to an class we need to declare it inside the constructor method.
Suppose I want to declare a variable.
Properties are basically a variable, nothing else.
So I'm going to declare a property late and our property name is name.
Then semicolon to end the line.
So here we declare a property.
And now I want to use this property I want to use this property in our message method.
So I'm going to remove everyone.
And here I'm going to use concatenation sign.
Also you can use Backticks here.
If you are familiar with template string method then I want to call name variable.
For that we need to use this keyword.
This means the object dot name.
Now we set a property in this class.
Also we use this property but we do not send any value.
Now we need to set a value to this property.
So how we can do it?
For that we need to use the object name in our case a a and I want to say it name name assign with and
here you can pass the student name which is Edward means and then semicolon to in this line.
And now again I want to print the message in my console.
So I'm going to uncomment this line and save this file.
After save this file, as you can see in my console now it's print hello at one means if I provide a
little space and save again now you can see it correctly.
Hello at one means with that also you can see another message from our constructor method.
And as I told you, we don't need to call constructor method.
It's automatically run when we create an object using this class.
And there is another shortcut that we can assign value to our properties.
For that here we need to take a parameter in our constructor method.
So I'm going to take a name as parameter.
And I'm going to assign a name to this variable is name.
And now we don't need to create this line to assign this value to this variable.
So I'm going to comment out this line.
And from here I'm going to copy add one means.
And whenever we create an object using this class we can pass the value.
It's like a function.
So if I save this file you can see the same result.
Oops I think I did some mistake because here we don't need to declare that keyword.
So I'm going to remove late.
This time I'm going to use this keyword, this dot name this keyword represent the object.
So if I save this file now you can see now it's print hello at one means it's pretty similar with function
is not it.
Yes it is.
Here you can define multiple properties as much you want.
Now let's talk about prototype method.
This is the example of prototype method.
For that you need to take the method name and prototype method.
Do not run automatically.
To run this prototype method we need to call it.
And our last method is static method.
To create static method we need to use static keyword.
Let me show you.
So here I'm going to create a method which is static method.
So I'm going to type static.
Then we need to take a method name.
In our case our method name is full name.
Then we need to use round braces.
Then inside the curly braces you can print anything document dot write inside the round braces.
Inside the double quotes.
Student name is Add1 means.
And now you decide you are going to call the static method from this object.
So let's call this static method a dot full name semicolon.
Do in this line.
If I save this file, it's not going to work.
It's going to throw an error.
Yes.
We don't need to create an object to call a static method.
We can call static method without creating a object.
To call this static method we need to use the class name in our case.
Hello.
So let me show you how we can use it.
So here I'm going to type our class name.
Hello dot full name function then semicolon to in this line.
If I save this file you can see the result.
As you can see in my browser now it calls the static method and print student name is add.
One means.
As I told you, class is a blueprint, otherwise a template.
We can use class multiple times to create objects.
So this is it for this tutorial.
Thanks for watching this video.
Stay tuned for the next tutorial.
In the next tutorial we are going to learn about inheritance.