Now what about methods? Let's say that our bellboy is able to do something called moveSuitcase.
Now, I've kind of simplified it here, but it's effectively, he creates an alert saying "May I take your
suitcase?"
And then he picks up the suitcase and then he moves with the suitcase.
Now this is a function because at the moment it's not attached to an object but in our previous syntax,
if we wanted our object to also have an associated function, then all we have to do is provide the name
of the function as a new parameter,
and then colon, and then after the colon we just put in the anonymous function.
And now our bellBoy1 has a method, which remember is just a function that's associated with an object.
And if we had a suitcase in the lobby, then we can simply call the method and say bellBoy1.moveSuitcase(),
and perform that method.
So remember that when we're using the properties or calling a method we're always using the dot notation.
And the only difference that you see between the method and the property are those parentheses,
so keep an eye out for those.
Now if we wanted to incorporate this method into our constructor function, then all we have to write
is
this.moveSuitcase is equal to a function that does these and these things.
And now when we create any object using this constructor function, then all of those objects will have
that function of moveSuitcase, and they'll all be able to do that.
So as a challenge, I want you to add a method to our HouseKeeper constructor function,
and this method is just called clean, so that we can get the housekeeper to perform the clean function.
And we're going to do it in a simplified way so that our method of what it does is sends an alert that
says "Cleaning in progress..."
So add that to the constructor function,
create a new housekeeper from that constructor function, and call the cleaning method associated with
that new object.
So pause the video now and see if you can complete this challenge.
All right.
So as we said before to create a new method or a new property, we first have to start with "this." the name
of the property or method.
So our method is called "clean" and it's not equal to just any value but it's equal to a function and
this function, as we said before, has to send an alert that says "Cleaning in progress..."
So now that we've got our method associated with our HouseKeeper constructor function, then we need to
create new objects that have this method.
So let's go ahead and again, empty cache and hard reload, and let's run our new code, and then let's create
a new object called houseKeeper1, which is equal to a new HouseKeeper, which has 12 years experience,
he's called James, and he's only qualified to clean bedrooms.
All right. So now let's hit Enter and create our new object called houseKeeper1.
Now, houseKeeper1 now comes equipped with this method called clean.
So we can simply say houseKeeper1.clean.
And if we hit Enter, you can see that we get an alert that says "Cleaning in progress..." Pretty good, right?
Now, at this point you might be getting a little bit of deja vu because it's looking pretty similar to
something that we've written before.
It looks almost exactly the same as these lines of code that we used to create a new audio
and to play the audio, because we created a new variable that was called tom1,
and it's a new audio object that is linked to this particular sound file,
and then later on we used that object's method called play() to play that sound.
So even though we can't see the source code of this constructor function, but we could guess what it
might look like.
So the constructor function for this audio object might look something like this.
So, as with all constructor functions, you start off with the keyword function and then you have the
name of the object that it's going to construct with the first letter capitalized, and then in between
some parentheses we have some inputs.
And in this case, it's going to be the location of the file for that audio.
So now our constructor makes a new property called file location which is set equal to the location
that was given when this new audio object gets created.
And then this constructor function also has an associated method called play(),
and this play function does a number of things,
for example it might tap into the audio hardware of the computer,
it might check whether the file at the file location specified exists,
it might check that the file is actually a sound file, and then it might try and play that file at that
location.
And all we had to do was to use that constructor Audio, to create a new audio object specifying the
location of our audio file,
then we simply called the method that was associated with that new audio object and got it to play that
sound.
Now a lot of the code that you'll be writing in JavaScript, involves using these properties and methods
associated with the objects on your website, and occasionally you also need to create your own objects
in order to get some custom behavior on your website.
Now hopefully, this lesson has helped you to understand what we've been writing
when we created our audio objects and when we called our methods, or when we used our properties in our
drumkit website. Now, a lot of these concepts are quite abstract and it might take a little bit of repetition
and coming back to it a few times in order for it to start making more sense.
But as with all things programming, as we get more practice using a lot of these concepts, over time it
will become more and more familiar to you and the puzzle pieces will start coming together.
So don't worry if some of this stuff is going over your head. We're going to be revisiting this in the
future
as we keep building more and more websites. But this is a great place to just sow the seeds about objects
in JavaScript, and hopefully, you'll revisit this lesson a few more times and eventually getting really
comfortable with this concept.