02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Object Methods

Its time to tie everything we learned so far together as we create our first method. a method is a fancy way of saying function but its a bit more then that. in this video we will cover and look into the relationship functions can have with objects and when they have a relationship we change there names to methods.

Video Transcript

00:00All right, so we can see that this whole thing works and you can see that a couple of stuff
00:03So one is we saw our
00:05How to create new properties we saw how to access those properties in that in the object that we created in the exact same way as we
00:12access thing
00:13access things and objects that already were created before such as the
00:17Canvas when we were playing with the canvas such as the date when we were playing with the date and then we saw also that the
00:23Dot syntax doesn't have to be only one leveled
00:25We could see that we went into our date and because our date was a type of object as well
00:28We went one dot further and called a function within it
00:32And now that I'm saying we called a function within it that actually has a name as well
00:36So when a function belongs to an object, it has a new name. What is that name?
00:40The name of an object that belongs to sorry a name of a function that belongs to an object is called the method
00:46So when you say the word method a method really is just a fancy way of saying a function that belongs to something
00:52Alright, so I want to create now also a function. So in this case in scenario
00:56I know I'm gonna need to use this text over and over a lot of times and I don't want to
01:01Reconcatenate all this copy each time and notice by the way if we're already talking about it notice that I'm concatenating strings with strings
01:08Although the last one is a number, right?
01:10So basically I'm taking a string and I'm putting it inside of that quotation mark. It's very important
01:15You don't put it you're gonna get all those errors
01:17and we're concatenating them together using the regular plus that we use also for mathematics and
01:23Some languages by the way, it's not the way that you would do it
01:26But it's not that hard to figure it out you if you look at PHP you use a different combination
01:31All right, so you just find what the combination is
01:33But most of the languages have the basic way of working very very similarly
01:38There's a few different variations and differences in the way you create an object or variations on some languages
01:44Don't even have objects. So it's really down to the technicalities of different languages change drastically probably in the extremes
01:51but if you go to languages such as JavaScript and an action script flash and
01:56Java you're gonna sound find a lot of similarities some differences as well, but a lot of similarities
02:02Okay, so our next goal is to actually create a function. That will be a method
02:06So before we create the method itself, I want to create the function and I'm gonna call the function is
02:13Tell me
02:16That we'll just call it. Tell me alright, so tell me itself is gonna be our little function here
02:20And our little function just for the sake of the example
02:23I'm just gonna do a return grab everything from inside here
02:26Put it first right inside because it's the same type of function. We've created earlier and then we'll fix it and make it work in
02:33An object or object way where the object is interacting with each other. So that was our first step our first step
02:40We just created a function. Let's just see that our function works
02:43We see that everything is continuing the work as we expected it to work
02:47Now the next step that we could do is I'm gonna show you two ways of doing this
02:51So one I could go to my object
02:53I'm gonna do the first one and then delete it and move on to the next one and the first way is I'm gonna
02:57Give a variable name. So I'm gonna call it a similar name. We're gonna call it the same name
03:02so maybe I'm gonna I'm gonna I'm gonna actually call it there is already a function called to string and
03:07I want to take that to string and override it with our function
03:11so I'm gonna go to my to string function that already existed before and
03:15I'm treating it as a variable as you can see and
03:18I'm gonna say to my to string that tell me you're equal tell me from now on
03:23Now that I've done that and notice that here I'm using my function as just the same way as I was you use a variable
03:29So I'm accessing it in a similar way. I'm basically overriding the to string function
03:35And putting into it the to tell me function that is an external function to the class
03:40To the object. Sorry. So now when we're gonna do our trace
03:43I'm gonna just trace out our object and you're gonna see something really cool. So first of all, I'm gonna trace of the object to string
03:50I'm gonna run it and let's see what happens
03:53So first of all, you already see that our to string is working our to string is working
04:01It's doing exactly what we want it to do
04:03We have a lot of problems with it that we're going to sort out
04:06And by the way of an object by default if you don't even say what function you want to call by default
04:11We'll call the to string function
04:13It's basically when you're trying to when it tries to convert it into a string because when we're sending it to the output
04:18When we're sending it to the display, we're turning it into a string
04:21So even just by calling the object directly look what will happen our object will automatically
04:27Call that function so we don't even have to call the function just by calling our object itself
04:31We're actually calling that specific function that we want it to call
04:34But let me just comment here that we could have also just called object to string
04:39Just in case you're looking at a source code, and you need a refresher
04:43All right, so this was one way of doing it. So we're just gonna mark here
04:49One way well, we'll just mark your one
04:52Alright, and I'm gonna comment this all up and let's look at option two. So option two is very similar
04:58So I'm gonna grab and by the way, there's a lot of issues with this
05:01So this is not the final code. We don't want to do this because if we create another object
05:05It's gonna you we don't want it to use the same thing, right?
05:08We want it to use its specific properties that are related to that specific object, right?
05:14All right
05:15So now the next step that we have here is we want to actually connect that to string directly to the function very easily done
05:21All I'm gonna do is I'm gonna delete this line push up my function up and by the way
05:26This is called creating an anonymous function. Why do we call it an anonymous function because I'm gonna delete here this function name and
05:33Basically this function format is anonymous. It doesn't have a name. That's why it's called an anonymous function
05:39Now this anonymous function is then assigned directly into ours to string
05:44It's almost the same thing only a little bit more fancy and a little bit closer to this object oriented model
05:49That we're trying to achieve and get close to and again just to test it out
05:54If we rewrite refresh our screen, we're gonna see that our screen is still working in the exact same way
05:59It's giving us the output that we would expect it to give

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!