Transcript of the tutorial.
TOP
All right, then.
So we have our user object right here and it has all these different properties.
But now I'd like to add some kind of methods to this object as well, so things it can do.
So how do we add those?
Well, we do it in very much the same way.
We just use a key and value pair.
So say for example, I want to add a method called login.
Then the key is going to be login and the value is going to be a function.
And inside this function we can do something.
Now all I'm going to do is console, dot log the user logged in.
Okay.
And now this is our method attached to this user object.
So essentially the name of the method is this and this is what it does.
So now what we could do is call this method by saying user dot login like this.
So save it and refresh.
And we can see this thing way over here.
So this kind of thing might look familiar to Remember when we use things like string methods, we could
have a name.
So I'll say const name is equal to Mario and then we could do something.
We could say name dot two upper case and that is a string method, right?
It looks very much like this dot notation.
First of all, whatever the object is or the value.
So user and name, then dot, then the method name, then we invoke it with parentheses.
So that is the same kind of thing.
So hopefully now this kind of helps you with the idea of methods.
Methods are still just functions.
This is a function, but it's defined on an object.
Much like this too.
Upper case is defined on a string object built into JavaScript.
Okay.
A regular function isn't defined inside an object.
A method is.
So what if we want to add more methods?
Well, let's do a log out one and this is going to be a function as well.
And inside here we'll just say console, dot log and the user logged out so we could call that as well.
I'm going to say now user dot log out like so and save refresh.
Let's have a look over here.
And now we can see the user logged out.
Okay, So say we wanted to make now a method which prints out all of these different blogs, say all
of them.
There's only two.
But either way, we want to log out the blogs to the console.
Well, what we need to do is, first of all, create a method and we'll call this log blogs.
So this is going to be a function.
And inside this function, we somehow need to now get hold of these blogs so we can log them out.
So how are we going to do that?
Well, to do that, we're going to have to understand a keyword in JavaScript called this and we'll
talk about that in the next lecture.