Creating And Working with Methods
A method is a function that is owned by an object. Before we move back to building a Google map we will see how to create methods. When we create our first method we will create it in the prototype property of JavaScript objects. At this stage it will be a magical component that would enable us to add new methods to objects. We create our method and now it is available to all instances of the object type we added this prototype.
Once we create and see how to create a method we will return to our Google map sample and extend our functionality of our application by creating a custom object that would control the creation process.
Video Transcript
00:00Alrighty, so before we move back into continue building our Google map, what I want to do
00:05is I want to just continue talking about one more thing inside of our element.
00:10So far we've seen how to create new properties inside of our object-oriented new object that
00:15we've just started working with.
00:17I want to show you how to create auto-methods or you know, not or methods functions to
00:23our student map.
00:24To do that it's quite simple, so I'm just going to delete everything here that we don't
00:27need just to clean our space out a bit.
00:30And right after we define our new student map, what I want to do is I'm going to take that
00:34student map, which is going to take that variable, and I'm going to type dot prototype, p-r-o-t-o-t-y-p-e,
00:45student dot, student map, whatever element, whatever function, and every function has this
00:49prototype property.
00:50And notice how I'm doing this right underneath it because it's more or less like building
00:54a new class.
00:55So we're building a new function for a new method for our student map.
00:59All that's left for us now is to create that method.
01:02So I'm going to just give a method such as make gray.
01:08Alright, so the make gray function is our method, whatever you want to call it, oops, make gray,
01:14and then we'll do equal function, and then we close down our function, there we go.
01:23So we've just created a new function which is called make gray.
01:25And really the style of it is we put the name of the function, in our case we're turning
01:30it into a constructor, we're turning it into a pseudo class you could say, and then we
01:35type prototype, which is now kind of like this magical word that we'll explore more
01:40deeply in a moment, and then we type in make gray, which is going to be the name of
01:45our function.
01:46Then we set equal, and then we put an anonymous function, and by the way this is the construction
01:51of an anonymous function.
01:52You put the name function, but you don't give it a name, because the name itself is
01:56going to be actually the value that comes back, and again because functions are very
01:59similar to objects, they're a type of object.
02:03Alright so let's make this actually work.
02:06Now what I want to do is I want to revisit our, and don't worry about the prototype
02:10for a second, we'll dig more deep into it in a couple of moments.
02:13So what I want to do is I want to rebuild this application area, what I want to do
02:17is I want to move things out.
02:19So first of all, I want to really create this map always inside of my constructor.
02:24So I'm going to cut this out, I'm going to go right into our element here, we could
02:29delete that student name because it's not really saying that we're using.
02:33We could probably upgrade our version now that we're actually starting to do
02:36something inside of our class, and I'm going to just paste inside my map.
02:40Now I'm going to change this variable to be not a local function variable, but to
02:44be in the scope of our object.
02:46So I'm going to change that to be this.map instead.
02:49Everything here is more or less good, our only problem is that we really don't
02:53want to create a student map that's explicitly connected to our ID, nor do we
02:59want it to be too explicitly connected to a specific point, because what if we
03:02wanted two student maps on our stage at the same time.
03:06So to fix that problem, what we're going to do is we're going to add a few
03:09properties into our constructor.
03:11One of the properties is going to be our longitude longitude, so I'm going to
03:16call, well the first one is going to be our div ID, so div ID, and the second
03:20one is going to be our lat long, so I'm just going to call it ll.
03:24Next what we want to do is we want to, in our student ID map ID itself, we're
03:28going to send in our map, which is our ID, and we're going to send in this
03:32new variable that we've created earlier.
03:35Now nothing is going to work at this point in time, so we're going to have
03:39to fix things up a bit before it actually is going to work.
03:42First of all, for our div ID, we can now change inside of our get document,
03:47get element by ID, just to send in that ID that is being sent through.
03:52The next thing is our actual center point.
03:54We want to change the variable name to be ll for latitude longitude.
03:59So far so good.
04:00Now we have everything here inside looks to be fine.
04:03Our only problem is that we still have your loose end variables that we
04:07just don't want them here, so actually all of this we want to remove
04:11out.
04:12Cut that out.
04:13So all we really want to do is create a new latitude longitude, create a new
04:17student map, and send our map and our new point.
04:22Our next step is in our make gray.
04:24I'm going to go inside of here, paste everything that I have here, and
04:27just go through and change my definitions from map to this dot map.
04:32So I'm just going to add there this dot before and the same for line 28.
04:36And again, because it totally connects to that scope issue, we want to make
04:40sure that we're referring to our specific object, to our specific
04:44variable.
04:45In our case, we want to set it to this because we want to actually refer to
04:48the object we've created inside of our student map, our map variable,
04:52which is an object as well.
04:54All we have to do now is save it, and if we go to our browser and
04:57immediately click refresh, we'll see that our map now will become
05:00colorful, and that is because we separated between the steps.
05:04Now if we look at it one more time, we'll see that we still have the
05:06make gray function, but we're not calling it immediately automatically.
05:10But if we want to, we could now call right underneath SM make gray, turn
05:15it gray, go back into our application, refresh it, oops, that's the
05:21wrong browser, go back into our application, refresh it, and now it's
05:24going to be gray.
05:26Now just for the sake of this application, make gray, you know, now
05:28that we're separating the steps, how about we add, and again, it's also,
05:34we're running through these stuff a lot of it because everything you
05:36could do with functions, you could do in methods as well.
05:39So just as much, let's say we want to add here another property which
05:42will enable us to change the saturation level.
05:45So I'm just going to call this variable SAT for saturation, so let's
05:51call it SATU actually, so make sure that no one gets confused, and I'm
05:54just going to change the value here from minus 100 to SATU.
05:58And now when I call make gray, now I could send a variable.
06:00Let's say I'm going to send 50.
06:01The values that are valid are between minus 100 and 100.
06:05Making it 50 will make everything super, super colorful.
06:09So I'm going to refresh this, and we should be getting a map that's
06:11very, very vivid, and we do.
06:14All right, so that covers our basic steps of an object-oriented
06:18programming.
06:18Now we know how to create a constructor.
06:21We know how to create variables.
06:23We've just learned how to create prototype methods.
06:27By the way, before we close this topic up,
06:29we could also create prototype variables.
06:32And the way we do that is, again, through student map.prototype.
06:37We could then create a new variable, and we could crawl that 1.002.
06:44And then we could probably delete this variable
06:46that we were creating.
06:47There are basically two ways of creating both functions and properties.
06:52And when I'm saying that, it's true, because if I come inside of here
06:55and I just type this dot, and we'll call here output version,
07:02and I'm just going to set here anonymous function, again,
07:05function, and just immediately.
07:07And all this is done inside of our constructor.
07:10But what I could do is basically console,
07:12I'm going to do it quickly, log, and I'm just
07:14going to output the this.ver.
07:18All right, it's going to open the log into the log.
07:21Now if to just, oops, let's do this again.
07:25Somehow I deleted it.
07:27Console.log, and we want to console.log.ver, save.
07:33And let's just add that in line four.
07:37I hate this error message thing that it overlaps our code.
07:40Why did I get an error message telling me
07:42that there's no error?
07:43Very, very annoying.
07:44So you could see our sm.output version.
07:49We could call it, run our application, go inside, refresh.
07:53And then we could see in our script
07:55that our version is 1.002.
07:58Now with that said, we just learned two distinctly different ways
08:03of creating both methods and properties.
08:07One is just putting everything inside of our student map.
08:09Two is doing it through prototype.
08:11Now right now we don't know what the difference
08:13between prototype and doing it all inside of our constructor.
08:17In our constructor, we created a new property.
08:20We created also another new property.
08:23We created a method, which was called output version, which
08:27we just tested out.
08:28And the same level, same token,
08:30we created a property for our object.
08:32And we've created a method for our object using prototype.
08:36And so far, you might be thinking, well,
08:38I'll just choose whatever is more convenient for me.
08:41But actually, there's a difference between the two.
08:43And we will learn much more about them as we progress.
08:46I first want to start with the actual how to do it.
08:49Then we'll start talking about the theory.
