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

JavaScript Scope

As promised in the last bio lets talk scope. We will talk a lot about scope and until you feel very confidante with scope in JavaScript you will not be comfortable as a Object oriented JavaScript Developer. One of the most critical key things you want to know really well is what scope is and how it works in general. So far we worked in an OOP world but so far we didn't own any of the objects and as such we didn't focus to much on scope. Now that we are moving deeper into OOP as we are creating our own objects we need to understand how scope works within objects. By the end of this session you will have a much more clear understanding of what scope is and how to solve issues when you loss you scope.

Video Transcript

00:00Alright, so, so far you were working in an object-oriented world, only that object-oriented
00:05world belonged to the DOM, it belonged to that window, it belonged to whatever you
00:09were in, but now that we're working in object-oriented programming, now that we're creating our
00:15own objects, now that we're creating our own constructors, it's getting more and
00:19more critical to understand these variations in scope.
00:23Now just as a recap, and a very, very quick one, because this is something that you need
00:26to know already if you're in the stage of your learning.
00:29When we created this variable, the scope of this variable, because it doesn't belong to
00:34an object, it's a floating thing that belongs to our function, the scope of the variable is
00:39the function.
00:40Now if this variable was defined outside of the function, and then we would just add content
00:47in it, then the scope would be of the window, because that's the object that's in the,
00:52it's the hidden object out there.
00:54Alright, so we basically added a new variable to the main object, because we did it outside
01:00of our function.
01:01With that said, you don't have to worry about those nuances as much when we're getting
01:04into this stage of our learning, but what is very important to notice is, every time
01:09you create something, what does it belong to?
01:13Even our object, even this function that we've created, it belongs to something.
01:16It belongs to our main, to our window in this scenario.
01:20Alright, but we're creating within it a new object, and whenever we're referring
01:24to things inside of it, we could refer to it, because as we're standing inside of
01:29this function, it's nothing yet.
01:32Only when we create that new student map, then we have something.
01:36Now this is not a reference to this function.
01:39It's a reference to the object that is basically, that was created in line 18.
01:45Thus when the object is being created, it's referring to itself when we're
01:49setting it to be this.
01:50Now it's a very, very important thing to know what this is, and we're going to
01:54explore it more in the next two hours.
01:56It's a very critical keyword.
01:58And this keyword always refers to the nearest object, alright, and sometimes
02:03there is no this, but in our case, for most of our lesson, we're going to always
02:07encounter this as belonging to a specific object.
02:10And anytime you get lost, one of the best things you could always do is just
02:13output out what this is so you really confirm that you're really in what
02:18you thought you were in.
02:20And with that intro, we completed creating our constructor, and that is
02:24absolutely amazing.
02:26And the next step after creating a constructor is starting to add
02:29properties into our object.
02:33So, so far we created the constructor.
02:37We've seen how objects work.
02:38So when we looked at the Google Maps, we saw that one of the objects that we
02:42send in was a literal object where we defined all of its properties.
02:46Now you might be thinking, okay, so now you know how to create a student map.
02:49How do you add properties to that student map?
02:53Now there's a couple of ways of doing it.
02:55We could do it directly inside of our student map class.
02:58So if I'm going to, sorry, constructor, all right, so I'm going to delete this
03:02line 24, and in line 24, what I want to do is I'm going to type in this dot,
03:07and I'm just going to add a new property.
03:09So I'm going to call this map name, map type, let's do this, map type.
03:19Map type equals Google map.
03:24So I created a new Google map, supposedly a new variable inside here.
03:28Let's create just another one.
03:30So this, let's do version for this class.
03:32Our version is going to be 1.001.
03:37All right, so that's our build version of our map.
03:41All right, and that's enough for now.
03:43Now let's go back outside, and what we could do in our console,
03:46we could comment down that this, and let's do a new console.log.
03:52This time around, we're going to refer to our student map,
03:55and then I'm going to type, and you can see in Adobe Dreamweaver CS6
04:00that you're going to get the actual variables that were dynamically created.
04:04So in our case, we created the map type for version.
04:08So I'm going to click on map type, and I'm going to just save that,
04:12go to my browser, and I want to see that I'm getting that map type
04:15when I refer to my object.
04:19And again, we created the object, and we could see that Google map
04:22is coming out in our output, and it was created in line 20,
04:26and that's absolutely true.
04:27In line 20, we outputted out the value that's inside
04:30of that specific variable.
04:32So the way we create variables in JavaScript in our constructor
04:36is by setting this dot, and then setting it up.
04:39Now, what would happen if we would create a variable
04:42and we wouldn't add that this?
04:43So if I created here a variable just by defining var,
04:46and let's say I would call your student name
04:51equals then phala.
04:55What would happen if then I'm going to save this,
04:57if I go back into my log, and I'm just going to comment out this log,
05:01and I'm going to start a new console.log.
05:05Again, sm, you'll see that first of all, I'm not getting the hints for it,
05:08a.k.a. the variable doesn't really exist externally.
05:11Now, if I try to force it, and I try to access it,
05:15even though it's not there, we're just not going to get it.
05:18We're not going to get an error, but we're just going to get undefined.
05:20So let's check this out if I refresh this.
05:23And I'm going to look at it, we're going to see,
05:25we're going to get undefined.
05:26And basically the reason is it's because we never defined this variable.
05:29This variable never belonged to the object.
05:32It was only a local variable just as much as our center,
05:35or a gray, or gray style and map,
05:38were variables that were local variables for that specific function.
05:42But they did not exist externally in the application itself.
05:46So this is local only in function, right?
05:50Well, everything above here is object variables.
05:56All right, what we're basically doing is we're taking the object
05:59that was creating and adding into it these variables, all right?
06:04Now, there's another way of adding variables to an object.
06:09And before we explode, actually, there are a lot of different variations
06:14and a lot of different ways to add code inside of JavaScript.
06:17We're going to focus on one streamlined variation.
06:21If you want to learn more different ways, there's a lot of strategies.
06:24JavaScript is a very, very flexible language.
06:26We're going to stick to a few different styles of work
06:30because we're pushing our direction towards inheritance.
06:34We're pushing our direction towards prototyping.
06:37And that's really our goal.
06:38Our goal is to get to the more advanced features.
06:40But if you want to explore more deeply,
06:42there are a few other ways of adding variables to objects,
06:46such as our direct variables when we add them to our object directly,
06:50when we're creating a literal object.

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!