Scope and This
This is where we really first start talking about OOP and objects. In this video we will learn a bit about scope and learn a bit more about working with objects using the 'this' to refer to the current scope.
Video Transcript
00:00Now, we have only one problem. We actually have two problems, and before we fix the problems,
00:04I want to show you what the problem is. So, I'm going to copy this whole thing again,
00:09and I'm just going to comment it all out. How are we going to do this? All right,
00:16we're going to have to do it step by step. There you go. One, two. All right, we're going to go
00:23right up to the top here, and what I want to do is I want to create a factory. We're going
00:26to create a function that creates objects. So, let's call it create schools or create
00:36teacher profile. All right, so we're going to create a teacher profile function.
00:42Now, the create teacher profile is going to basically create that object that we were
00:46creating earlier. So, what I want to do is I'm going to paste all that good stuff that we've
00:50created, and by the way, we could put functions inside of the function. That's completely fine,
00:54even though it seems like not, but it actually is. Let me just delete all the extra buff that
01:00we don't need, and there we go again, and instead of alerting our object, we're just going to
01:08return that object. All right, so what we did here is we're creating basically a factory,
01:13which it's called this way, a factory, because we're creating a function that will create our
01:18object type that we want. So, in our scenario, for example, let's create a profile for Ben,
01:22and obviously, right now, we're going to want to modify a few stuff such as maybe when we create
01:26a teacher profile, we want to send some information into it. We want to send the name of the teacher,
01:33which is me in the scenario. We want to send the name of the school, which in our scenario is
01:39zero to geek, and we want to send out to the age of the school. The date, we're going to just
01:43hit it. We're going to create it dynamically. You know, we don't need to send that as a property,
01:47because we could just see what the date is currently. All right, so now we just will
01:50add those properties into the function. So, we're going to put your name, school, age.
01:59All that's left for us to do is go into here, type your name, go in here, type in your school,
02:05and last but not least, type in your age, and now it's all externally controlled. All that's
02:10left for us to do is actually test this out and see if it actually works. To do that, I'm going
02:15to do a new alert for Ben, which is our new object, because it was returned to us in the create
02:21teacher profile. Let's test this out and see if this works or not. So, first of all,
02:27we could see that it's still working, which is really great. So, we're getting ahead.
02:32Now, let's create another teacher. So, we're going to create another teacher, and we're
02:35going to call this teacher Sharon, and Sharon is my sister, by the way, and my sister
02:40doesn't get work for us, but let's say that she does, because I've been trying to convince
02:43her to. She has a master's degree in mathematics, and I would really love to have some master's
02:48degree level mathematics on my site. Sharon, I'm going to leave Sharon for now, because she
02:54has a new last name, and I don't want to reveal it. I'm kidding. It's Levee, but
03:00well, until she's a teacher in our school, we're not going to use her full name. There you go,
03:05sister. So, and she's also going to be part of our school that exists for 24 months. Obviously,
03:09this is a very fictitious example. So, let me just run this quickly again. I'm not going
03:13to call Sharon for now. I'm going to call Ben again. Ben is still working, and Sharon is going
03:19to work as well. Now, fortunately for us, this works. Unfortunately for you, it works as well,
03:25and the reason why I'm saying that is because although this works, it is mighty confusing.
03:30This is very confusing because of this baby here. In other words, referencing the object,
03:36although it works, and the reason why it works is because this is all pre-configures inside
03:41the JavaScript, and it knows who do we mean when we say object. But a better way and a smarter way
03:47would be approaching the object without actually saying the name of the object.
03:52The reason is, is very simple. Imagine that this function that is right now internal here
03:58was external, just for the sake of the example. So, if I come here and I take that beautiful
04:03function, and I quickly just move it out just the way, excuse me, which we called it tell me.
04:13So, if I take our tell me function, if after I removed it from the area there, and I just
04:17set there as the variable tell me, and I run it one more time, I think we're going to have a
04:22surprise reaction. It's not going to be what we hope, and here we go. We're not getting what we
04:26would have hoped to get. The reason is very simple. Because we're referencing a hard coded
04:32value. When we have our function external to our object, when we're not creating an anonymous
04:37function, it doesn't really know what this is. It doesn't really know, I'm revealing the magic word,
04:43it doesn't really know what the object is. The object doesn't exist. That name obj only exists
04:48when we're inside of the scope. So, if I go back one more step, even though it was working,
04:52and I move it back into where it was, what I really want to do is I want to say the name of
04:57the object without actually saying the name of the object. And lucky for us, there is a really
05:02really great way of doing it. Very easy to do it. And it kind of opens the door, blasts the door open
05:08to something new that's called scope. And we're not going to dig too deep into scope yet, but we
05:12definitely should. And you definitely should have an awareness of scope. And I'm just going to take
05:16two words about it before we do it. Scope in essence is when you're in a program, you need
05:22to know where the program is. So now that we're working with objects, and some objects belong to
05:28things. When we say the word name, now explicitly, we will be approaching the scope that it's
05:35currently in. Now, what is the scope that it's currently in? The scope that it's currently in
05:41is that object. So even just by removing the object, the school, and the day, and you know,
05:46just approaching it directly, in theory, everything should continue working, at least in theory.
05:53But technically, it doesn't. And the reason is, again, because it loses that scope.
05:57So we need to figure out a way how to make sure that when we're saying this variable name,
06:03that JavaScript understands who are we talking about. And the way we do that is really simple,
06:09we're just going to type the word this dot. And this basically means I'm talking to myself,
06:14find the property name within me. All right, we're going to do that for the school as well.
06:21We're going to do that for the get time as well. And if we run this one more time,
06:28we are back in action. And we could see that char works and plays at zero to geek,
06:33and the time now is yada, yada, yada. And our Ben will work as well. So we learned a lot of stuff
06:39right now. So I just want to recapture it quickly. We covered a lot of topics. So first of all,
06:45now we know how to create our own buckets, our own objects, we know how to add properties to
06:50those objects, we know how to access those properties, we know that we could put complex
06:55variable types in an object. And by the way, we could create put complex object types of our own
07:01type. Alright, so for example, if I wanted to, I could appear property, which was maybe every
07:07teacher was was this teacher referred, put a fourth parameter and the refer would be Ben,
07:12so Ben referred Sharon. So in this case, if we just do this really quickly, object ref refer
07:19would be ref. And we'd put here ref property. And all that's left for us is maybe to send Ben
07:25inside. In the case of Ben, nothing will be sent. So the ref would be equal null. But that that's
07:31that's all we had to do. And now we created a complex object that stores elements that are
07:37using our function more than once. So we created something that has a much deeper layer of
07:42complexity. So we learned a lot of stuff here. And the last but most important thing is we
07:47learned that we could create our own properties, we could create our own methods, we've seen how
07:53to access methods by calling this to string function, we've met how to access it also in
07:57previous videos. But in this video, and the most important critical part is that we started talking
08:03about scope. And I'm not expecting you at this stage to be scope masters. But I really hope
08:08that at this stage, you have a understanding of what that actually means. And if you don't,
08:13please don't continue and rush through the course. Just send me an email.
08:18Watch the video one more time. It doesn't work. Send me an email. And I promise you that
08:23we will make sure that you understand it. And if you don't understand it, then, you know,
08:27I'll make sure that you understand it. Like, I'm not going to give up on that,
08:30because it's really important. And I really encourage you throughout all our courses, don't,
08:34you know, it's part of your membership on our on our site zero to geek, take advantage of it,
08:38please take advantage of it. That's what I'm here for. You know, I want you to succeed. So
08:42please do, you know, and it's fine, you know, there's there's no, there's no dumb question.
08:48Only dumb question is the one that wasn't asked. Okay, so always ask. And that really covers this
08:53topic of a lot, we just covered a lot. So we talked everything. That's it. That's all I have
09:00to say about objects. So I get excited. There's a lot more you could do with objects that I'm
09:04not even going to talk about. In the next couple of videos, we're going to meet a couple of more
09:07complex or more advanced type of objects and another couple of ways of accessing functions.
09:13But you're already already we could say you're, I wouldn't say a programmer at this stage or
09:18a JavaScript programmer, but you're not that far away. We're, we're so close. Alright, so
09:23I hope you're having fun. And in the next few videos, obviously, we'll try to tie it in more to
09:28our live project.
