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

Scope Open live Panel

In this video we do an open pannel with our students and talk about scope learning together through live questions on the meaning of scope.

Video Transcript

00:00Now I wanted to start talking a little bit more about scope. We've talked about scope a lot of times
00:05but it's already at this point. It's really really important for us to get what scope really is and
00:10The best way to explain it if we go back to all these stuff like we talk about animals
00:15We said like a cat could breathe it has legs it has eyes it could sleep it can make noise and and in this natural
00:22environment that we're talking about we know that the scope of the of the eyes are of the cats because why because this
00:28Belongs to it's a belongs to relationship scope now sometimes that belongs to relationship is really easy to determine and
00:36To prove it that it's really easy. We're gonna ask you guys first a question now
00:40We didn't we talked about it a lot throughout the developer basics. We talked a little bit about scope in other places
00:45Now I'm first gonna ask the question and then I'm gonna turn to our chat and we're gonna ask someone to answer this question
00:51So first of all, we have your dog rabbit cat. They all belong to something right? It has a scope
00:56what is the scope of all these variables and I'm gonna randomly select someone and
01:03Our someone is gonna be Danny. So Danny who does the dog?
01:10Well, obviously if I ask you about who does the dog class belong to it?
01:13It'll be easier, but if I ask you, what is the scope of the dog?
01:18Well right now we're in the main class so right now we're in the main class and we have a variable forget about what's
01:23Inside of the dog itself, but where is the dog inside?
01:26Really, that's a question that you're asking yourself when you're asking about scope where it's where is he where is he at?
01:32Where it what is he in is a good answer to scope?
01:38Beautiful. All right. Perfect Adam. All right. So now I'm gonna ask Adam the eyes that we have here in our o dot eyes
01:45What is the scope of eyes?
01:48To whom does eyes belong?
01:53fantastic, and and maybe instead of and
01:56Yeah
01:58Yeah, but that it already doesn't matter because you're right the eyes belong to the object so it's that's that's a perfect answer
02:03That's the scope of eyes now. I want to ask Carol even though we didn't
02:09Randomly select or Carol who does who Oh belong to this is a really hard one. You know what?
02:16Maybe we'll randomize it because it is the hardest question
02:18So but if you don't Carol, oh and you know, you were supposed to answer because you're the one that came out
02:23Okay, so let's give it a give it a try and you know it and and if not, then someone else could join it
02:29But to give you they have it a little bit of an intro
02:33So we're saying that Oh, well, I'll just tell you that Oh does not belong to the main
02:39Okay, that's where it gets a little bit more tricky, which is the next level of scope. It actually belongs to
02:47Something else it is
02:53Well, no that Oh doesn't belong to the animal cuz we're inside of the main class right now, but you'll notice that
02:59The oh the oh the object itself a variable. Oh, it's okay
03:05We talked about it in the past, but we talked about it in the functions developer basics and it's been a while
03:11No, don't worry guys. I'll you know, let me take the chat away. So you don't don't worry about it
03:17Let me let me start digging into this is important topic to talk about it. It's part of the developer basics book
03:22We didn't really have a big reason to talk about it too much until now because we weren't really dealing too heavily with objects
03:28But now we are and now when I'm working with object my object belongs to always really the best answer is and let me you know
03:36Like I'll put the chat up anyways the best way to answer this question when you're inside of a class
03:40Go to that specific object, right and just click on your command comma
03:45your command comma is going to select the closest opening and closing curly brackets if the closest opening and closing curly brackets are
03:54is an object or a class or a
03:58function it belongs to that
04:01Instance now you might say but why those and why not another thing and and we actually we talked about this in one of the
04:08Videos and I just want to show a little sample here
04:10If I say you're in this is important to really get if I'm if I just create your even around this
04:15Far, I'm just gonna put here an if
04:18True then we're gonna do all this now
04:25You might have thought that well you said curly brackets and if I put a curly bracket here
04:29then it seems like that this var belongs to the if statement, but it's not true because an if is a statement and
04:37If it's not a something ifs can't contain things things can't belong to things can't belong to something
04:43That's that abstract as the word if they can't belong also to something that abstract as a loop a for a while
04:49It can't belong to a switch
04:52It can't belong to a for it can't belong to anything. That's not a thing switches. Don't hold data
04:57They just they're like traffic control. They just move things from place to place
05:01So so when we did this in the first bracket we saw okay
05:04So this variable didn't belong to that bracket it didn't belong to this it can't be it can't belong to this because if doesn't
05:10Have things it's not a thing. It's a command. It doesn't have any ownership of anything
05:14So I took a step up above now I hit a function
05:18When I hit a function what happened this function could it contain things? Yes, definitely because it does contain things
05:24Oh
05:26Functions are buckets of variables and things and they do things functions as their nature
05:31They could store variables and they could manipulate variables and thus a function is a great host for our object
05:38Now on the other hand if I would know let me just before we continue does that make it night?
05:43Is that logical that is the question? Yeah, that was a bad oopsie, but they editing you know no worries
05:53Okay, guys does that make sense so like if now I'm gonna
05:56I'm gonna put you on the spot again, and I'm gonna ask you another scope question. Will you be able to solve it?
06:01Do you guys think?
06:04Excellent all right, so I'm gonna before
06:07We're gonna find out right now, so let me just you know
06:10I'm gonna clear all this up and let's say I'm gonna create another for loop, and I'm gonna say crater of our eye and
06:17Equals zero eyes smaller than 10 I plus plus and then that for look I'm gonna create for loop
06:23I'm gonna create a switch, and that switch is gonna have
06:27Only one is gonna have a few cases gonna have case zero. It's gonna trace in case zero narf
06:34And then it's gonna break out
06:37Let's go completely random, but that's fine case five and all the rest is gonna
06:43Go just go to the default and we'll make sure that the case five
06:46We just don't do anything and the default will be it's not
06:53It's not zero
06:55It's not zero
06:57And we're even gonna create here another variable var
07:02Message oh, you know yeah, let's do that var message string equals. I'm just gonna take everything inside of here
07:07But then they're
07:11Good, so we created a nice amount of variables and things here just about enough to start bugging you with
07:18What belongs to what you know let me just add one more and?
07:27I would be honestly
07:31But
07:36Obviously this doesn't make any sense the sample, but it's really just to play with the idea of scope in its basic core all right
07:43So we're looking at this as sample
07:44I'm gonna start asking you questions about the different variables to who do they belong so let's start let's uh
07:50Randomly cycle between you guys again and see who is the lucky person to answer yay Danny supposed answer, okay, so Danny
07:57Let's start with the hopefully easy one this time, but maybe not the variable message
08:04To who does the message belong?
08:09Yeah, I know I'm very nor friendly. I could tell you in the meantime while Danny is doing is I used to know it started
08:17Programming I used to curse in my code
08:19you know when you're sitting in your programming for 12 hours, and he's like I trace out fuckwise in this working and
08:24I learned that that's not a good idea and since then I
08:29Avoid doing that because you never want anyone seeing you cursing in your code, so I just use north
08:36What can't you see Danny?
08:37So it's a good. It's a good tip. Don't curse in your code very important
08:44Could you move the mouse down yeah here? We go my house is right next to where it's supposed to be
08:51Yeah, you know like ever I think everyone does that but like especially when you're working in corporate uh the corporate world
08:56I have to you have to forget about that habit. It's such a bad bad habit
09:00So just find like a word that you like I like the word north and I just use it each time
09:04When I'm frustrated, or I don't have what to say
09:08Which is from pinky in the brain, so
09:11Danny got an answer for me
09:15Me fantastic all right perfect
09:17It still belongs to that main function and and the reason is like we said we went one bar up nope can't belong to us
09:23Which which can't own things went back one step four can't own things either so we want one more step back
09:28And yep, that it that's it. It belongs to the main
09:32Great work. All right. Let's let's um. Let's um
09:35Definitely a huge win
09:37All right, so let's cycle it up again. See who's the one this is kind of fun right we're random isling
09:43Uh-oh now. I can't do that again. That's just not fair all right this random tool needs to be a little bit more smart
09:49Okay, Adam here. We go all right, so Adam your question is
09:53Let me give you a hard one. Does it who to who does the variable I belong to?
09:59Let me put my mouse right next to it, so you can see it
10:07Really, you just say now control correct
10:10What are you talking about?
10:16You're next you're next Carol main function as well. Yeah. Hey, you're right now that you could you explain why oh?
10:25It's control. It's a command and then comma
10:29Command comma will select everything between two
10:32Brackets, which is really really useful on a PC. Oh, I guess it would be one of the buttons that are not going to command
10:39like the shift no like the
10:43You guys have no idea which one it is, but it's one of those buttons are next down there next to the
10:48Yeah, exactly
10:50How many options you have thank you I try I try
10:54Yeah, Bingo exactly it's another bar
10:58It's a little bit tricky just because it's inside of the forest you might think that the scope of this bar is supposedly inside of
11:03This four and to that point so let me just ask you a follow-up question if I created this variable here
11:08Right and we exit that for loop does the variable still exists where it when we're here where we're in line 30
11:15Does my variable I does it still exist?
11:19Well think again because you ready you gave the right answer before Danny. Yeah, correct
11:26But why why does it still exist it still exists because just what you said Adam
11:31You said that the variable I its scope is the main is this main function thus if the scope is the main function
11:37That line 30 is still inside of the scope of that main function
11:41Okay
11:45Yeah
11:46Okay, great now Carol is your turn now. This is maybe harder or maybe easier
11:51Carol what all right? Let me just do the random thing even though you know it doesn't make oh here
11:56We go what a surprise okay, so Carol
12:02Bingo Bingo so Adam that is a great great great point so if I create here another variable I create another var I and
12:11Because I'm creating a few very let's you even if like let's say
12:14You're creating another loop so until now you might have just ignored those errors right, but those errors
12:20What were they telling you they were telling you excuse me?
12:23You can't create another eye that I already exists now. It's not gonna. It's not gonna fail your application
12:28It's still gonna run. It just creates a
12:30Runtime it's kind of like a compiling error, but not really it's a warning okay, so this warning says duplicate variable definition
12:37Why is it a duplicate because they're created in the same function?
12:41Now speaking of that if you already brought that up
12:43What is the solution to that, but I still need to create an eye
12:46I don't want to create a new variable name for each for loop all I have to do is instead of creating the eye again
12:52Just use the eye
12:53I don't have to define a new variable in my for loop
12:55I could just use the eye
12:56Reset it to zero because it was ten right now because when we were done with it
13:00It was equal tense. We have to reset it again, so we could use it for another loop for another round, okay?
13:09There you go, so you just you just saved a variable
13:13Good stuff, and that's exactly where scope comes into the picture and and you could stop ignoring those warnings
13:18So from now on try to avoid those warnings because they're good for you that won't break your application
13:22They're just telling you hey by the way you created another variable, and it's the same name
13:25So maybe that's a bad idea all right, so I want to continue in this line of thought so back to you
13:30Carol and the question to you Carol is
13:33To what to whom to what to what what is the scope of the variable message one?
13:42Main what is it the class or the function?
13:50Mm-hmm well, where is it defined? That's a question everybody even though we're using message one inside of the constructor inside of this main function
13:58Where are we actually defining it? We're defining it up here in line 13
14:03So if I pointed my finger, and I said and I said okay
14:06Who does this belong to you wouldn't say that but because you were focusing on line 18 then you said that so let's try this
14:12Again line 13 is where we define the variable
14:16Beautiful okay, so exactly it belongs to the main class and deliberately
14:20I made a little bit more confusing right if I if I wanted it to make it easier to notice that I would say
14:24Well, I would define a type for it. I would say okay this because it's a class variable it belongs to the class now
14:31It that connects to the fact that when we're creating and and let me ask you
14:36More more questions because we're already in a place where we're ready for questions
14:39And I'm gonna ask you questions, and then we'll answer if we don't know the answer so let me just cycle between
14:44I
14:45Would like to enter the room okay cool
14:49Hey, Ramaz welcome to the room all right, so Carol, okay?
14:53I won't do this to you again, so let me cycle between and ask someone else a question Danny here. We go so Danny
15:00my question now is
15:02Why why don't we define?
15:04Private or public to variables that we create inside of our functions, so I'm inside of the function
15:11Why don't I just so call it private?
15:14Private bar, and so I'm not allowed to but why don't I do you have just an instinct of why not and?
15:21And if not if you don't know then anyone else could join in and answer
15:25That is so beautiful Danny. That is really really great answer
15:32So their scope can only be accessed from the function exactly there
15:35No one else could access this anyway, so there's no point in saying public private. It's it's private by default
15:41No one could not only is private as soon as this function is terminated this variable doesn't exist
15:45anymore
15:46On the other hand a variable that's defined in the class itself exists forever
15:50It exists forever as long as this class exists, and it's thus that it's it's externally accessible now
15:58Because now it has a name it's externally. I could say main dot whatever that variable is sorry for clapping my hands
16:04It was a bug flying in my face
16:06So now I have to say okay, no I don't want no one else seeing us
16:09I'm going to set this to be private so really great great answer Danny alright
16:13So that covers our our basic talks about about uh
16:17About uh about scope, but let's take one step further

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!