Going back to Variable scope
Scope is always going to be our main topic. In this part we go back to our earlier question and try to really understand the scope of our variables in the for loop.
Video Transcript
00:00Now now going back to your question we're talking about and let me just push this up to the top
00:04So you're talking about scope of variables, which is a very very important topic
00:08Which the more you work with it the more the easier it's gonna get but when always when you ask yourself
00:14What I what is I who'd first ask before you ask for where is it gonna be defined?
00:21The first question you want to ask yourself to who does I belong so I'm gonna ask you that question to who does I belong?
00:27But we said that for isn't we said that for isn't a thing. We said that for is exactly like if statements
00:37It's just an operator. It's just a
00:40Commander, so we can't say that I belongs to it because it doesn't it can't hold anything. It's not a variable, right?
00:48So who could it who I belongs to right now? No, that's fine. I belongs. Well, we're not in a class
00:59We're not in a class right now in a scenario like this when we create a variable
01:04It's the exact same thing as if we've created here in line 13 and in line 13
01:08Who does I belong the answer to that and we didn't work on it?
01:12We didn't talk about in these videos, but you could always find that out by just asking doing a trace asking. What is this?
01:19Wherever you are wherever you put the this usually you'll know who the host is but that's not up and that's not 100% accurate
01:25So, you know, let me not say that whoever knows what that means and they'll check that and they'll trace this but a
01:31Variable and I'll say the answer to that variables always belong to the closest scope to them
01:35And what does that mean a variable could belong if it's inside of a function if it was defined inside of a function
01:41Then it belongs to that function. So if I created here a variable and I called it a
01:47Then a belongs to the function me as soon as we leave the function me a doesn't exist anymore. Okay
01:54Now on the other hand if I create another variable and I call this variable B
01:58But I create this variable outside of that function now this variable doesn't belong to the function
02:03It belongs to a bigger scope and that bigger scope in this scenario because we're not in a class is actually our whole application
02:09now on the other on the other hand if I just go jump just for a second into I'll create a class here just for
02:15Just to show this inside of classes because we're trying to do these stuff inside of a class
02:20Right now we've been working on the timeline just to make things easier and quicker
02:24So we don't have too many things going on
02:26But I just want to add this because it's important
02:28So we're gonna go into into edit mode and I'm just gonna make sure that's in flash and I'm not gonna even say this
02:33So let's assume that we have a remain that it's saved all this time
02:36We've been working in our constructor right here, right?
02:38We've been working inside of these areas and we've been ignoring all the rest
02:42Whenever we created a variable in here though that variable where did it live?
02:46It lives inside of the function now that we know what functions are. We know that it lives inside of that function
02:52Even though we're not covering it in this round of videos if I created a variable up here
02:57And I just want you to guess this because we've been taught saying that it's always owned by someone if I created a variable up here
03:02It's outside of my function area, but it's inside of a different block of code, right?
03:07If I mark that block, it's inside of something else to who does be belong to?
03:15Beautiful, exactly. So our B belongs to the main whenever we're in a function
03:18The B belongs to that function and that's really the most important thing about scope and a lot of times when developers
03:24It
03:31Actually flash automatically creates a class that's not visible to you
03:35Even if you don't create one and it manages internally
03:42Then that's where be it belongs to the root
03:44It belongs to and I would say and that's where you the best way to answer that question is you can always just do a trace
03:51And I'll just show you exactly how this works if I do a trace right here
03:54And I and I just run this for a second
03:58We're gonna get a trace and let me just fix this name conflict because we've created too many things here
04:03Created I a couple of times. So let me comment that out
04:08All right, so we had there that one two three which is fine and we're gonna ignore all the embed stuff object main
04:15Now if I deleted the class main from here and I didn't have the word main right and I just
04:21Ran it one more time and I go right up here
04:24Now it says object main timeline, which is the exact same thing only because we didn't define it. I just gave it a name of its own
04:32Okay
04:33No
04:46Yeah, and that would be a fine answer that would be a great answer
04:48Yeah, it but it belongs to something it always belongs to something
04:51Even if we don't tell it to belong to something because flashes an object oriented program
04:55Even if we don't treat it as an object oriented program
04:59Everything inside of flash belongs to something besides a very very few amount of things which are called global
05:05Variables and global functions which are very rare. For example, the trace is a global. It's a global function
05:11It's a it's global because trace doesn't belong to a specific object
05:15You could call it in any object and it's always available
05:18But those are more rare whenever you create a variable on your own and flash
05:2399.9% of the time it belongs to something
05:26Okay, okay
05:28Now we covered a few bases here. No before I ask you another question. Do you have any question right now?
05:44absolutely
05:49Definitely
05:51Definitely and I would say that you know
05:52Like some projects just are easier to do on the timeline than then do them on the timeline such as projects that are all about
05:58Timeline animations so sometimes you'll want to code on the timeline
06:02But the advantage of putting it in classes is one getting used to you working with classes
06:06Which is really really important because those few the the the more videos you watch on the site the deeper you get into
06:12Programming the more you're gonna need to work in classes anyways
06:16So if you could work in a class then work in a class if you can't then work on the timeline
06:20Which it really depends on the project
06:21But ideally you want to try to work in a class and the biggest reason for that is because it makes it easier for
06:27That for you then to reuse your code. For example, if you have
06:31You don't even need an example. You could reuse it for different types of banners
06:35You could be could be you need to make a banner now for the same client
06:38We need to make five different banners and they all act the exact same way, but they have different images
06:44If your code was inside of your application, it makes it a little bit more tricky, but it really is each case on its own
06:50Alright, so do you have any any other further questions in the meantime?
06:59Mm-hmm. Okay. So answer that question to who does I belong?
07:11No, I didn't create it, but the main class or the timeline are both a good answer, but it's your main class
07:17Definitely, but if you created that loop in in a different class
07:20Then it would belong to that class and as soon as it was created you have a class created
07:24It's it's just a naming thing. Like we said variables have different names
07:28So if we call it main timeline or we can't call it whatever you want to call it doesn't really matter
07:31It's just it's our main class. Okay, okay
07:35And what I wanted to do next is and and I feel like you're really you're doing well here
07:40So I'm gonna I just want to make sure just want to look at our site for a second
07:43I'm gonna open it up on the side of yourself have a reference to see all the stuff that you need to know about
07:48in the developer
08:05No, not really because let me ask you a question, okay
08:08Var equals your I equals zero in line 11, right when you first started how much would it be equal when we're in line?
08:1614 well, let's check. So if I'll do your little trace here. I
08:313 and the reason why it's 3 and not 2 because 2 is still valid so 2 went inside and whenever it goes inside like we said
08:38As soon as the forest when we get to this point, then we this command happens before we check this
08:45Exactly
08:51But it's a good question
08:52So let me just repeat that just because it's a really good question
08:55So in the point in this scenario when we first go into the for loop we defined it as zero
09:00So we defined it already here so we don't have to define it again
09:03This is basically only first time as it happens only first time gets the final key
09:06So it's equals one that's still valid. So it starts the loop when it completes a loop
09:11No matter what it goes to this area. It adds one
09:14It could it completes all the stuff that are in this section when that is done now
09:18This doesn't exist anymore. This happens only once and if it makes it easier to see it
09:22You could say okay, you could take this line push it up up here. This is valid as well
09:27Okay, so this will happen only once in line 10
09:31Now we're not doing anything in our initiation and then we first go as soon as we go into our application
09:37We're not doing anything first. We're doing the trace
09:39Well, if it's valid then we do this at the end and we check this at the beginning
09:44So we do this at the end of the for go back to the top check if this is true go to the end
09:50Go ahead
09:50So even though this is that this is if I would want to make this easier to view and this is not
09:55Synthetically correct anymore. I could take this as well and put it just here to make it clear that or more accurately
10:02Well, yes, this is probably the right position to put it. So I
10:06Would be zero we're checking if it's true it is we're gonna do this now
10:11We're going plus one and going back up checking if it's true
10:14Doing that and so on and so forth until a time comes back then we go back up and it's not equal anymore
10:20And then we just step out of that loop
10:23Okay, okay, so I completely messed up the sink
10:27It doesn't this is not this is not valid code
10:36This is just to make it easier for you to see but but if I put this back up here
10:40Then this is valid code
10:42because
10:44Okay
10:45and the reason is and
10:48But I'll tell you something that is valid code. I just want to show you
11:01Yeah, exactly. And I want to show you just one more thing. So just to show you how this would be valid
11:06I'm just gonna take this down here. And now this is the exact same thing and it's gonna be valid
11:11So I'm just gonna run it to make sure that I really could I'm gonna really gonna get traces from zero
11:15And let me just comment this out because we want from zero to two. It's supposed to be right
11:19Let me just save that run it and we'll see that we're getting zero through two and it's still fine it and so this is valid
11:25So this is actually exactly what is happening inside of the loop and this is probably more readable when you're first starting to do it
11:31But obviously when doing it more compact is just the way it's done and it's easier when you get used to it knowing that
11:37This is your first time this happens every time in the end and this happens each time when we ask
11:41When we ask permission to go inside of this area
