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

Excuse me, how wide are you?

Up until now we approached objects and functions directly and that was very useful until now. For us to be a able take the next step as developers, we need to create code that is more reusable.

A good example to this would be, a great teacher that sits and teaches a student and gets paid 50 bucks. Another teacher, that is just as good, creates a video course and then sells it to masses for 10 bucks. While both may be great teachers, one of them is more efficient and thus more profitable as he helps more people. This is exactly the same thing as what we're going through right now. Up until now, most of our developments were a one time hit, but for us to become great developers and leverage this new skill, we need to learn how to reuse our code.

Video Transcript

00:01Don't worry my Elton days are over
00:03I actually wanted to leave these glasses on because I thought they're a great segue to the topic that we're going to be talking about
00:09so in this chapter, we're going to start seeing functions again, and
00:13the most important thing you want to get of it is just like these the glasses although
00:18Theoretically there are Elton's they could have been mine and really because they're a piece of plastic
00:22We could duplicate it a thousand times and reuse it the idea of functions is exactly that
00:27You don't really want to create functions although maybe sometimes you do that are a one-time use only like a one-time use cup
00:35The best functions are functions are like a glass cup that you can use over and over and over or these glasses that they're a template
00:41And hundreds of other people could got get themselves glasses just like that
00:46And if that makes sense this lesson is going to make a lot of sense. Let's jump right in
00:51What we want what I want to do next is I want to make this more dynamic
00:55It's going to take us a few lectures to get there
00:57But the first step I want to do is I want to take all these items put them into a separate function
01:02Let's give that function a name something that would make sense such as maybe
01:07Expand expand right how about that so we'll create a new function. We'll call it expand right
01:14And for now, we're just going to put everything inside
01:17So I'm literally just going to copy all the stuff
01:19We've created so far and I'm just going to paste it right into that function
01:23All I need to do is call expand right make sure it runs and once I do that now
01:29Everything is going to be inside of a function kind of like getting a reminder of the course
01:33We just created about functions in general now the next step the next step that I want to do is
01:39I'm going to go back in here and
01:41One thing that I want to do is I don't want to use static
01:45References to size anymore. So what I'm going to do is I'm going to use the jQuery
01:51jQuery, I'm going to use jQuery to help me define it
01:54So the first step is I'm going to create a new variable and I'm going to call it X for the X position
01:59You could call it left. You could all whatever you want
02:01I'm going to set the value of 0 in it now
02:04The first step is my X will be equal 0 the next step after that. I'm going to want to basically define
02:10How much does it grow each time?
02:12So literally after we animate our first block in after we animate our first block and I want to make sure that my X is
02:19Going to grow so I'm going to do plus equal which is a shorthand to adding to whatever value is already in our X
02:26And what I want to do is I want to go and approach my block that I've just worked with
02:38I'm going to wrap it around the dollar function, which is the jQuery function and
02:44What that is going to do is going to turn this into an object with a few more properties
02:48Such as the width function and the width function is going to return to me the value of the actual width of this block
02:54So now my X value has grown by that X position
02:58now all that's left for me to do is go ahead here where I set my left in my date block and
03:03Instead of that just combine that value that I have with the characters PX to make sure that we're sending in pixels
03:10into our element
03:25Literally all we need to do is again copy this value again right after that and make sure that we're up
03:30updating our X with the update with the size of our date block and
03:34That will add that width of the date block into our X as well
03:37And now I could also literally just go ahead there and add X to that element as well
03:43Now notice how I could do plus minus inside of properties
03:46That's completely legitimate because it's bounded it knows where the next property starts
03:51and that's where that comma is or you knows when the item is over when the semi when the
03:56curly bracket is
03:58There so though that's it really that's all we have to do to animate our element without defining the actual width directly
04:05Now obviously if we go into our element right now and we click on refresh
04:09We're gonna see that it's not gonna animate the way we would want it to it's gonna be a lot more
04:13Overlap than we would want it to be and the reason is we didn't put any spacing here
04:18So how about we add some padding or spacing into our elements by adding another property that we could call?
04:24Spacing or padding. How about we call it padding?
04:27So we go up here. I'm gonna create another variable and I'm gonna call it padding and
04:32For padding. I'm gonna put here any value. Let's say I want to set a spacing of
04:37Let's say 20 20 pixels each time now. All I have to do is each time I add a value here
04:43I just want to also add the padding again
04:45I want to add the padding each time I'm updating my x position and now when I do that if I run my element
04:50Just one more time. We'll see that now our
04:53Function is gonna run and it's gonna have the space that we want our element to have maybe 20 was too much
04:58How about we we probably want to notch it down to 18, but even before we do that
05:02how about we go back into our element because we said we want to make things dynamic how about we add the properties of
05:09padding into our element, so I'm gonna add your padding and I can said the default is 20
05:15So I could literally delete this padding because I don't need to define it again
05:18I could change that maybe to even 17 and set there 17 as my default now if I click on and I save and
05:25Save my element and I run it again
05:27We'll see that the the function will automatically put there 17 in there
05:31But if I want to put less I could without any problem go ahead into my editor and instead of that send hey
05:37I change my mind
05:38I only want it I want it to be
05:4040
05:41Pixels my padding and if I do that we'll see that we're gonna have a lot more padding than we had previously
05:46All right, but obviously that's not our goal. We might want to go there and set in a different value
05:50I'm gonna go ahead and just set it to be 15 and that will be the end of this element of this
05:57Lecture, but we're definitely not over because we still have a lot of things to do to make this dynamic
06:03Now one of the things that we're we have here that is problematic that makes whatever we're doing right now and not dynamic is
06:11Well, actually there's quite a lot of stuff
06:13The one thing is is that we're referencing our elements directly
06:17So if you look into our code, we'll see that we're referencing what we're animating and what we're changing
06:22in a very direct way inside of this function and in other words
06:27We have this is explicitly only useful for the properties that we have in there
06:32But what if imagine we had here a lot more elements that we want to animate?
06:36What if our we had your these properties
06:39But we had also a new another few properties or beyond that
06:42What if we wanted to use this function again and another project right now would be impossible because we're referencing directly to our elements
06:50inside of our function
06:52what we really want to do is we want to figure out a way how to
06:55Extract that information and send it out to be one of the properties we're sending in but that would be a problem because if we
07:01Do that we're gonna have a few problems. One is we're gonna have a problem of
07:06You have to put a lot of properties here, right?
07:09So what is this is a problem? So before we solve this problem in the next lecture
07:14We're gonna learn about something that's called a raise with and another something, but we'll start with a raise
07:20So in the next lecture, we'll learn about what our raise are and then we're gonna take advantage of their unique capabilities
07:25So we could solve this problem
07:28Alright, so we'll see you in the next lecture where we learn about a raise and then continue improving our function to make it more
07:34Anonymous so then it could be more reusable. So see you in the next lecture

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!