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

this

What is this? Reconnecting with scope and looking at 'this' for the first time.

Video Transcript

00:01The next thing I want to talk about is that this what is this and we're kind of
00:06reconnecting and retouching with this topic of scope which is going to follow
00:10us throughout a lot of courses in the future as well. And scope is basically
00:15how does flash know or how does our code know where is what. And as we
00:22create different variables and different objects and different
00:25properties, it's becoming even more critical to understand what is
00:30our scope. And to sample this out is what I want to do is I want to create a
00:34property for our hello which will be instead of Ben let's say here we're going
00:38to create a property which will be called message. And in our message we're
00:42going to type here hello which is our message. Now in my trace method right
00:48here I could have just said o.message right but then I'm linking my
00:55function with that specific object which in our scenario is not the end of the
00:59world but if we run our application again we're going to see hello. Now what
01:03would happen if we didn't know that we could well not only we didn't know we
01:07usually try to avoid inside of functions inside of methods to call
01:12something explicitly the name of its parent. And the reason is or its
01:17definer and the reason is is very simple. We want our functions to be able to be
01:22anonymous and reusable and we don't really want to reference the things
01:27outside of those functions if we could avoid it. And Flash has a few ways to help
01:32us avoid using explicitly the word o inside of our function. And just to make
01:37sure that we understand the scope issue if I create a new variable here which I
01:42will call message as well and I could do that because oops not a stack frame
01:47it's a string. Now we're going to type here no no no. And if I just try to trace
01:57out from my function message what my what my function now is first of all it's
02:02checking to see is there a definition of message inside of this function? No. Is
02:06there inside of this function? No. Thus I'm going to look outside outside of my
02:11scope. I'm going to look out into the bigger area and in this bigger area I'm
02:16going to find a message so I'm going to output message. So if I run this app
02:19again no no no and it's finding my string. Now if I would comment that out it
02:24would generate an error right because it could not find it. How could I let Flash
02:29know from inside here without saying without actually saying it without
02:33actually using the o as my reference? How can I make my function know or my
02:38message know that it belongs to whatever container it's in? So the
02:43container is our object and the and the keyword and I've said in the beginning
02:47is actually this this dot message. What does this mean? This is kind of like a
02:54dynamic value. It's an it's a dynamic object that basically this is always
02:59what defined it. So in inside of our function our definer is our o right so
03:06this is o when we're saying it inside of our function inside of our method
03:10because methods methods belong to something. So our this is basically it's
03:16like saying what do I belong to? So what whoever whoever I belong to dot
03:21message and this dot message should output a low and it does so but if I
03:27would grab this trace and trace it out outside right here what do you think
03:32would happen? If I trace this out right here let's see what happens bam there's
03:36an error. Why is there an error? You might be thinking but I defined here a
03:41variable you definitely did but that does not belong to there are this are
03:46this what is our this? This variable is not part of a global scope this variable
03:53belongs to our function but our function is not an object it's just a function
03:57this function belongs to a bigger object which is called main and thus our main
04:04does not have a property called message our function does but not our not our
04:11function not our main class not our big object now this could get confusing but
04:17let's see how this works again let me just so this we say we can't we well
04:21this won't work won't work as there is no message there is no very there is no
04:29property called message but only a variable right there's a variable called
04:37and here we're gonna we kind of see it more explicitly that although we do have
04:42a variable here but this variable is not a property because it doesn't belong to
04:46an object same deal here if I would create a variable right inside here it
04:50would be a variable it would not be a property and thus when we would use our
04:54this our this really could only reference different things inside of a
04:58specific object so if our this was here the long-term object if our this
05:03is outside it belongs to the bigger object and in our scenario it's
05:06actually our application it's our main which we didn't dig into and it's fine
05:11but let's see let's see what that is so if I just trace out right here this
05:16before we even get into our message if I just trace out this and let me just put
05:20here tracing from the main method right now we know it's a method it's not a
05:29function anymore and we trace out this we're gonna see that we're getting back
05:34an object of a main object right which is our main object and if we trace out
05:40from inside of our hello function from our hello method and you'll notice there's
05:48a lot of people can get confused with it because it's fine calling a method a
05:52function is not a sin calling a function a method could get you confused but
05:57functions are functions and methods are type of functions only methods belong to
06:02something they have a this they have a belonging they belong to something so
06:08now if I run up my application one more time I should be getting that our hello
06:13method belongs to the object object now let's just see one more thing if I create
06:18here another function one of those traditional functions that we've created
06:21before function it's kind of like a function inside of a function right let
06:25me just make a screen a bit bigger so I'm gonna create a function here that
06:30it's gonna be test this and it's not gonna return anything and we're just
06:38gonna trace and trace this is not a method right it's a function I'm gonna
06:50set that as a function and I'm just gonna call test this and let's see what
06:54what does this return and try to think about it what do you think is the
06:57logical value that would return as this well this test is not a function
07:04right where do you think it's gonna find the value of our this and there you
07:10go and basically it's telling us that it doesn't really have a this and it's a
07:15global this and this our global this is basically there is no scope our scope
07:21so if we try to use this inside of a function that actually is not a method
07:26we will get confused although we're getting something back here this
07:30something that we're getting back is not exactly what we would expect right so
07:36it's very important to notice that when you're fun when you're trying to trace
07:39that this out of a function you're gonna get ran well not random things we're
07:44gonna get things you're not expecting and if you work as you work with this
07:47and you get more comfortable with this it's gonna be much easier but it's in
07:51the beginning time and it's again with these are one of the most most common
07:56bugs and flesh or especially in an earlier versions of as to and it
08:01definitely now as well is that people get confused by what is this and that
08:07this is always for what we're concerned right now are this is always the
08:11connection of our this between our our method and who it belongs to and thus
08:17because this is not really a method we're getting something unexpected but
08:22this is a method then we're getting what we expect and this is definitely a
08:27method which is our class definition and we're getting the value that we would
08:31expect as well okay so that's my that's my notes about this and don't worry too
08:37much about this we're gonna definitely meet a lot a lot more and we're gonna
08:40see how powerful this is especially as we work with multiple things at the
08:45same time when we don't have to explicitly say what this is

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!