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

Passing Params to Class

In this video we learn how to pass parameters to our new created class enabling our classes to be more dynamic and easier to control.

Video Transcript

00:00So let's say we're just in details, just one more time.
00:03So first of all, if we look at it from our app perspective,
00:06we just hid all of that information out of our app.
00:09What did that enable us?
00:10That enables us, for example, to create two timeouts
00:13if we wanted to.
00:14In the same visual display, we could create two of them,
00:17three of them, 100 of them, it doesn't really matter.
00:19Now when we're looking into our timeout display,
00:21let me just scroll up here,
00:23in our timeout display, what we have,
00:25we have, first of all, it's beautiful,
00:27it's small, it's less code.
00:29We have the exact same logic that we were working on before,
00:32so if you even grab the source files just to review it,
00:35because I know we just copy-pasted and moved stuff around
00:37and we didn't dig into the logic.
00:39But even if we do just quickly,
00:41we have here two variables, one is public,
00:44which we've seen before,
00:46and one is private, which is our timeout.
00:49And in our timeout display, in our constructor,
00:52we automatically are building out a new timeout
00:54and setting it a value of three seconds.
00:56Speaking of that, that probably is a problem, right?
00:59Because what if we want to create two timeout displays?
01:01Let's do that just for a second.
01:02If I wanted to create two timeout displays,
01:05I could just copy this variable,
01:07and look how nice it is that now that it's private in here,
01:10no one has access to it because we're building it by code
01:13and we're not building it,
01:14we're not throwing it onto the timeline.
01:16So if we wanted to use it only in the future
01:18and after something happened,
01:20like after a user clicked on a button,
01:22which we looked at previously,
01:24we could have when the user clicks on a button
01:26only then the timer would start.
01:28We wouldn't have to worry about figuring out
01:29how to have everything working at the same time.
01:31It would only start when we need it,
01:33only when we call our constructor.
01:35So I'm gonna call MC timeout two, our second item,
01:39and all we really have to do
01:40is just copy this whole block,
01:43and I'm just gonna delete the Y from the first one,
01:45so one will be higher and one will be lower,
01:48and this time around,
01:49we're just gonna put it at like 250.
01:53And if you just run it now,
01:57I probably put it on too low,
01:59so it's probably way too low.
02:02So let me put it on 250, it was too much.
02:04Let's try 80, and there we go.
02:09So we have two timers.
02:10Now, obviously the biggest problem right now
02:13is they're all running,
02:13they're both running at the same time, right?
02:16What if we wanted to give them different times?
02:19So let's do that.
02:19The only part that we're missing
02:22is to be able to pass this three through our constructor.
02:25What we wanna do is we wanna take those three seconds
02:28and basically we would wanna,
02:30we probably would wanna just say,
02:32and you'd probably be thinking,
02:33hey, when we added in our timeout,
02:35we added to our constructor in our timeout,
02:37basic timeout, right in the amount of seconds,
02:40why can't we just go ahead
02:41and do that ultra-tire timeout display?
02:44Well, we could, there's nothing illegal about it.
02:46The only problem is it's not going to work,
02:48and let's see why it's not gonna work.
02:50I'm just gonna do it quickly just to show it off.
02:53The reason why this is not gonna work,
02:55if we go to our app,
02:57our app is not, we're never starting up the timeout display.
03:01The timeout display, if we go into our library again,
03:04F4, into our library,
03:07and if we right-click into our control-click
03:10into our properties,
03:11we're gonna see that we're not dealing with the base class.
03:14Our base class's set time is timeout display.
03:16That is our ancestor.
03:18So like our visual, not like,
03:20our visual display, our MC timeout
03:22is not an instance of the timeout display.
03:25It's an instance of the LMC timeout.
03:28And our LMC timeout, because we're not controlling it,
03:31we didn't build it on our own,
03:33our LMC timeout isn't expecting to get any parameters.
03:37So if I put here any parameters inside of my LMC timeout,
03:40if I run it, for example,
03:41if I run it without putting anything,
03:43everything will seem like it's working.
03:45But if I try to put anything into it,
03:46if I try to put a value into it,
03:48we'll have to save it first, obviously,
03:51confronted with an error telling me,
03:52whoa, whoa, whoa, what are you doing?
03:54We're expecting to get zero parameters.
03:56And it's valid because it's an incorrect amount of variance
04:01that our constructor for LMC timeout,
04:03which is billed automatically, is expecting zero.
04:07So how can we solve this problem?
04:08If we wanted to, we could have created a new class
04:10for this item and a new class for that item,
04:13but we don't really wanna do that.
04:14What is the easiest solution?
04:15The easiest solution is to go back
04:17into our timeout display
04:19and create an empty timeout display.
04:22So I'm just gonna grab and just paste it again,
04:25and I'm just gonna make it completely empty
04:27with nothing in it.
04:27We're not gonna do anything in our constructor.
04:30What we're gonna do is we're gonna take our old constructor
04:32and just call it in it.
04:34Now, we have a function that is not part of the constructor
04:38that we could call that function later on
04:39whenever we're ready for it.
04:41So in this scenario, if we go to our app,
04:43back to our app, and we try to call it,
04:45let's see what would happen in my generator scenario.
04:47Let's see what would happen.
04:49So first of all, if I just call my MC timeout
04:51and then I call in it, and I send it,
04:53let's say the value of 30.
04:54Let's say the first item will run or not.
04:56And we can see our first item is running.
04:58That's good.
05:00And we can do the same thing for our second item
05:02where let's say with 30 seconds we want for our first one,
05:06and our second item, we want it to run for a whole hour.
05:11So that'll be 60 times 60.
05:16So we're starting it, we're initiating it in a whole hour.
05:19And here you go.
05:20We have two timers running at the exact same time.
05:23One of them and both of them are counting
05:25completely separate times.
05:27And we could have one that completes
05:28before the other one completes.
05:29So if we just put here three again and we run it,
05:32we'll have our 60 minute one which will continue running.
05:36And we'll have our other one
05:38which has been completed already.

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!