Welcome back.
So this is our last video on javascript objects for the next few units.
And in this video we're going to cover javascript methods before we do that.
I just want to explain my set up.
I have a simple HTML file and all I have is a few notes here just for us to follow.
So you absolutely don't need to type this up if you want to follow along.
You can just do this in the console or make your own file but you don't need this page to him out.
So what I'm going to do is start talking about what methods are so in javascript.
We've seen that we can make an object which is going to make one called LBJ.
And we can put whatever type of data we want instead of that object.
So we could have a name that's a string Charke.
You can have an age that's a number we can have it is cool that's a boolean we can have friends which
is an array and friends can just be Bob and Tina.
But what I'm interested in showing you in this video is that we can actually add functions as properties
to an object.
In that case are actually called methods rather than properties.
So again a method is just a function that is a property instead of an object.
So I can make one here.
Let's just call it add add is a function and it takes two numbers x and y and all that it does.
If I indent this properly is return x plus y.
So the big thing that's new here is that we're just adding a function.
It doesn't matter what the function does.
Any function will work here and we're just setting it as a value for the property.
And so if I hit enter here and we look at RBJ you can see it's an object and we have aged 45 friends
.
An array is cool false name Chuck.
And then add is a function.
So if we wanted to call add we can't just write add anymore what we need to do is write obey j dot and
just like this and then let's give two numbers 10 and 5 and we get 15.
So calling it works the same way we need parentheses.
We need arguments.
The only difference is it's not just on it's own like this.
It's actually now instead of the OPG object.
So this might look a little bit familiar from something like this.
Contrl Balog exactly the same format.
And it turns out that the consulate was an object and log is a method on that object and recalling it
just like we called object dot AD.
So they work exactly the same way.
So you might be wondering why would we ever want to add a method to an object.
Why not just have our functions defined separately like this.
Why do we need to added inside of the RBJ object.
There's a few reasons.
The first is that it helps you keep your code organized so you can group things logically together to
illustrate one of the benefits of adding your functions as method on an object.
I'm going to do a quick example here.
So we're going to go back to our favorite example of cats and dogs and let's say that I wanted to make
a method called speak.
And let's suppose I wanted to make a function called speak and speak all that it needs to do is return
what.
So this is for a dog or wolf.
And that's it.
Very simple.
And I can call speak like this and I get what.
Let's suppose that I also want a method called Speak to work for cat and it should return.
Meow.
Well if I write function speak
and I return meow.
And this time I call speak I get Meow.
But now I have no way of accessing my original speak that returned Wolf.
So what happened here is something called a namespace collision.
So that's just a fancy way of saying that we have two different things that have the same name.
So if we instead added these functions as methods to an object we could have two different things named
speak.
By putting them in different name spaces.
So it's really simple to do that we could just make something called var dog space and that's just an
empty object.
And then we just say dog space dot speak is a function.
And we return what.
And we do the same thing for Vark cat space.
So Dog spacing cat space are just names of making up and they're just going to be empty objects that
we add our methods to.
So then I can say cat space dot speak equals function and all we do here is return.
Yeah.
So if I want to call speak for a dog I want it to return Wolf.
All they have to do is read Dog space speak and if they want to see me know all I need to do is run
cat space dot speak.
So again this is just a fancy.
So this is just a nice way of organizing our code.
First of all it's that we can have a bunch of methods that are grouped logically together so all the
dog methods go into that dog space all the cat methods go into that cat space.
But it's also a way to avoid these namespace collisions.
I know by now you're tired of dogs and cats and it doesn't seem like a very real world example but I
can assure you the same exact logic applies for something like comments and posts and tags.
You might have a method called new or delete.
For comments we might have one called delete posts.
Another one called Delete for users and we need to have those namespace properly so that they don't
all conflict.
So rather than just writing deletes we would have user delete and then we might have post-START delete
and comment delete and we will see things like that all the time when we get tabac and programming.
So to sum up what we've covered so far we're able to add methods to an object which means we're adding
a function as a property and we can add it just like any other property.
Treat it just like a string or a number.
And that's because functions are values in javascript.
We can pass them around.
We can add them as a value to an object which is really really useful.
The second thing we talked about is why you would ever do that.
The first reason we talked about is that it prevents namespace collisions which really just means we
can group code together.
That means that we can have properties and functions and methods that are named exactly the same way
except they're stored in different objects so that they don't conflict