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

objects

In this video we start building a project without thinking how we group them up(well we are thinking about it) in the end of this session we will see how much easier it is to group our code into buckets - aka Objects.

Video Transcript

00:00So let's dig a little bit deeper into this.
00:02Basically, I wanted to look at this in action.
00:05But what do I mean?
00:06Let's just create here a very, very theoretical application.
00:10Now, we want to create an application that
00:12mimics the behaviors of a cat.
00:14And I would love to have some interaction with you guys.
00:18We're going to take this to wherever you guys take it.
00:20So I'm going to go into the chat here for a second.
00:24We'll have to name it short name.
00:26So create, body, that's our function.
00:29OK, so we created our cat's body.
00:32What other stuff would we need when
00:33we're creating a theoretical cat that we're going to use?
00:36Anyone throw ideas, and we're just
00:38going to create a couple of functions.
00:40Cool, OK, walking.
00:41Definitely, we would want to have a cat
00:42that is capable of walking.
00:46All right, any more ideas?
00:49Let's say make sounds.
00:53So let's say talk, just to keep it simple.
00:58Let's say we have another function
01:00that would be maybe build head.
01:07Let's think, what other functions could we have here?
01:10So we have our create body, we have walking, we have talk,
01:13we have create head.
01:14Our cat needs to eat as well.
01:17Anyone give me just one more example?
01:20It eats, OK, cool.
01:22We think the same way.
01:23So anyone else has driving a car?
01:27I don't know if cats drive cars, but maybe, fine.
01:30Done, I'll take that.
01:32So our cat actually could fly, and it also could drive.
01:35So fly, and it could drive.
01:40Whoops, I'm used to drag and dropping,
01:42but I didn't create a drag and drop.
01:44OK, so drive.
01:45Now, we want to create a few more functions that
01:46are also more procedural, stuff that we'll never really
01:49use again, but we'll only create one,
01:50such as creating our body, or building the cat's head.
01:54So let's say adding fur for the cat.
01:56OK, this is just a random create fur.
02:03Let's say we want to create another one, another type
02:05of function that we really would only use once.
02:07Let's say we'll create a function that defines the color
02:14of the cat, define color, CLR.
02:17So we have a lot of functions, which some of them are,
02:21whoopsie, define CLR.
02:25All right, so we have quite a lot of functions.
02:27Some of them, obviously, are more usable.
02:29Some are less usable.
02:30And now, imagine that, let's say we created a real cat,
02:34and we had, like, 100 of these functions.
02:37And each one of these circles represents a function.
02:39Over time, as we get more and more functions,
02:42obviously, sometimes they call each other.
02:44So maybe this one uses that one,
02:45and that one uses that one, and that one uses the other one.
02:48And you could, obviously, already start
02:50imagining how stuff start getting all intertwined
02:52together.
02:53The more functions you have, the harder
02:55it is to then control and figure out
02:56which ones are actually talking with which,
02:58and how do I control and make sure
03:01that this circle is only going to be,
03:05it just is, now that we're only talking about a cat,
03:07imagine that we're building an application
03:09that has a whole zoo.
03:10So now we have 5,000 functions.
03:12Some of them are only for the cat.
03:13Some of them are for dogs.
03:14Some of them are for planes.
03:17And some of them are for other stuff.
03:20So obviously, we start getting a problem
03:23as we have more functions that are not
03:24actually related to each other.
03:27But all these functions that we created right now
03:29are all really related to each other.
03:31And that's kind of like really under the hood,
03:33the whole idea of object-oriented programming.
03:35That could be the summary of object-oriented programming,
03:38and anything beyond that, anything beyond that
03:41is really just a question of technicalities
03:44of how to actually do it.
03:45But the whole heart of object-oriented programming,
03:47the idea of it is really to enable us to create buckets.
03:50And now these buckets have to be,
03:52in some level, logical.
03:54Now, these buckets, usually the way we define it
03:58is we usually would organize things
04:00based on a category or we'd organize things
04:02based on what it is.
04:04So for example, in our scenario,
04:07all these functions are related to a cat.
04:10So we could say that all of these together,
04:12we could group them up into a specific object
04:14and just define that object as a specific object.
04:18So let's say in our scenario, crap,
04:21we have an issue here.
04:23I mean, oops, there are too many things
04:25we need to think about at any given time.
04:28I wish we could just bucket things together
04:30and put them on the side, well, not in the side,
04:33but you could put them in the side as well
04:34and not worry about them when they're not an issue,
04:38which is really an issue, right?
04:39If we have hundreds of functions,
04:41a lot of times we don't even care about those functions
04:43because we're not even thinking about them right now.
04:45We have a bug with our dog.
04:47We don't care about the cat functions,
04:49but we don't really remember which function is of the cat,
04:51which one is not of the cat.
04:52Things start getting really complicated.
04:55And that's really where our first solution
04:56with object-oriented programming comes.
04:58Its main goal is to organize our code into logical buckets.
05:01Many times these buckets can correlate
05:04directly to things in the real world,
05:06AKA organize things into objects.
05:09So in our scenario, that thing of the real world
05:12is our cat.
05:13I want you to meet Jerry the cat.
05:15Jerry the cat is a cat that has all these different
05:19behaviors, all these different things,
05:20all these different methods that functions, methods,
05:23which are as a name that you're gonna hear often,
05:27functions are basically functions
05:29that are not related to anything.
05:31A method is basically a function that belongs to something.
05:33So now because all these functions belong to the cat Jerry,
05:37now the cat Jerry owns all these different methods.
05:41Not only that, whenever we call the function edit,
05:45or whenever we call the function drive or the function eat,
05:49we don't have to tell the function drive
05:51inside of the function drive who should drive
05:53because the who is already clear in action script
05:57or in JavaScript, it would be called this.
05:59So basically everything that's inside of this element
06:02that we just created, everything that's inside
06:04of Jerry the cat belongs to Jerry the cat.
06:07Thus if I tell Jerry the cat to eat,
06:09that eat function is already aware,
06:12it's already part of that cat Jerry,
06:14so we don't have to specify who is supposed to eat.
06:17Now I wanted to make sure that this concept is clear
06:19before we move on to our next problem
06:22that object oriented programming tries to solve.
06:25Backlying, I missed that one, that's a good one.
06:27Let's see if I could add clawing still.
06:30Done, all right, let's add clawing.
06:31It's actually, that's the one I like.
06:34Scratch, scratch away, Mr. Cat.
06:39All right, so are there any questions?
06:41You guys are with me so far.
06:42So we, at this stage,
06:43we basically grouped everything together.
06:46Back to Flash, no questions so far that works for me.

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!