1 00:00:00,570 --> 00:00:05,880 Before we go back to your classes, I would like to talk about something called scope and scope is an 2 00:00:05,880 --> 00:00:11,760 important concept that you need to understand in order for us to move forward with classes. 3 00:00:12,390 --> 00:00:14,460 So scope is the following. 4 00:00:14,610 --> 00:00:18,990 So let's say I'm creating a function here, and I'm going to call this one my function. 5 00:00:20,610 --> 00:00:23,280 And here I pass a value. 6 00:00:23,520 --> 00:00:24,960 Hey, which is of Typekit? 7 00:00:26,310 --> 00:00:26,700 OK. 8 00:00:27,060 --> 00:00:35,130 So if I now try to assign a value to my variable a it doesn't work because it's not really a variable. 9 00:00:35,190 --> 00:00:35,730 This is. 10 00:00:37,160 --> 00:00:42,500 A perimeter so you cannot assign a value to a perimeter within the function. 11 00:00:42,770 --> 00:00:46,820 You can only has an argument when calling the function. 12 00:00:47,030 --> 00:00:51,140 So here I can, for example, pass five as a. 13 00:00:52,150 --> 00:00:52,570 All right. 14 00:00:53,080 --> 00:00:55,210 So you cannot reassign a value to it. 15 00:00:55,900 --> 00:00:58,660 Now let's say I create a variable called a in a year. 16 00:00:59,110 --> 00:01:00,190 What's happening here now? 17 00:01:00,910 --> 00:01:04,959 Well, this variable A is actually a variable. 18 00:01:07,330 --> 00:01:08,800 A is a variable. 19 00:01:10,650 --> 00:01:12,180 And now here, this a 20 00:01:15,000 --> 00:01:16,710 is a parameter. 21 00:01:19,060 --> 00:01:21,640 Now, there are multiple things that we need to look at. 22 00:01:22,360 --> 00:01:28,000 One thing is that this variable A, as I said, is not the same as this parameter A. 23 00:01:30,020 --> 00:01:33,830 So what I can do is I can say for a is going to be a. 24 00:01:34,670 --> 00:01:42,350 So now this a here, our power meter, whatever value it has, will be passed to our variable called 25 00:01:42,350 --> 00:01:44,510 a, which is this variable here. 26 00:01:46,290 --> 00:01:48,540 And now we can go ahead and. 27 00:01:51,440 --> 00:01:57,200 Code printed, so a is and a now, which of the A's will now be printed? 28 00:01:57,890 --> 00:01:59,370 Will it be the perimeter? 29 00:01:59,390 --> 00:02:01,010 Or will it be the variable? 30 00:02:02,470 --> 00:02:09,130 Well, it will be the variable we can see it if we just assign a value of four here, for example. 31 00:02:09,490 --> 00:02:12,670 So I'm going to assign a value of four to my a variable. 32 00:02:13,360 --> 00:02:19,420 And if I run it, you will see that a is four, even though we past five as the eight to it. 33 00:02:19,990 --> 00:02:20,770 So there we are. 34 00:02:20,800 --> 00:02:22,240 It says a is four. 35 00:02:22,750 --> 00:02:29,050 So now you might wonder why Scotland allow us to declare variable with the same name as a parameter 36 00:02:29,050 --> 00:02:29,830 inside the method. 37 00:02:30,670 --> 00:02:32,920 Well, somebody else have the same question. 38 00:02:33,280 --> 00:02:38,020 And here's an answer that was considered correct. 39 00:02:38,620 --> 00:02:43,570 This is called shattering, and it is useful for decoupling your code from other parts of the system. 40 00:02:43,870 --> 00:02:47,170 It is possible because names are bound to the current scope. 41 00:02:47,320 --> 00:02:49,870 So here we see this key word scope again. 42 00:02:51,510 --> 00:02:57,780 Now you can, of course, pause the video and read through all of this, but I personally like to go 43 00:02:57,780 --> 00:03:03,900 to the recommendations directly, which is number one, your code becomes hard to read as two different 44 00:03:03,900 --> 00:03:07,860 things have the same name, which leads to confusion, which already happened. 45 00:03:07,980 --> 00:03:10,500 So I think it's already confusing what we have there. 46 00:03:10,830 --> 00:03:16,320 And another disadvantages once shut out, you can no longer access the original variable within a scope. 47 00:03:16,830 --> 00:03:17,220 So. 48 00:03:18,370 --> 00:03:25,150 If you look back here, we have no way of accessing this variable anymore, except for if we actually 49 00:03:25,150 --> 00:03:27,790 assign it to our VAR A. and then use it. 50 00:03:27,970 --> 00:03:29,860 But then we could have gotten rid of all this. 51 00:03:30,070 --> 00:03:31,960 The whole thing from the get go. 52 00:03:32,260 --> 00:03:37,360 So this would work already or even without this VAR a equals A.. 53 00:03:37,750 --> 00:03:38,200 OK. 54 00:03:38,830 --> 00:03:40,660 So this is just one part of scope. 55 00:03:41,110 --> 00:03:46,330 Another part of scope is the fact that and I'm just going to create a different variable here. 56 00:03:46,330 --> 00:03:47,620 I'm going to call this one be. 57 00:03:48,990 --> 00:03:56,970 Our variable, be that we create here, we can use it easily within our function, right, because it's 58 00:03:56,970 --> 00:03:57,630 within scope. 59 00:03:57,840 --> 00:04:01,110 That's where it was created, so that's where we can use it. 60 00:04:01,560 --> 00:04:03,720 But we cannot use it outside of this function. 61 00:04:03,990 --> 00:04:09,660 So I cannot go ahead and say be is equal to three, for example. 62 00:04:10,080 --> 00:04:13,320 This doesn't work because it doesn't find to be unresolved. 63 00:04:13,320 --> 00:04:14,490 References what it says. 64 00:04:15,220 --> 00:04:23,130 OK, so if I create a local variable in here called be this variable D is going to be a very different 65 00:04:23,130 --> 00:04:24,930 variable than this variable B here. 66 00:04:25,530 --> 00:04:33,240 So they are all only visible within the scope of themselves, which is within the scope of their own 67 00:04:33,240 --> 00:04:36,990 function, or if it's within a class, then within the whole class. 68 00:04:37,470 --> 00:04:43,170 So if I create a variable B here and I want to use it in here. 69 00:04:43,920 --> 00:04:52,560 So let's say I want to assign variable B here and like this here, this is going to work because this 70 00:04:52,560 --> 00:04:58,020 variable is created outside of functions, so it's available within the same file. 71 00:04:58,230 --> 00:05:03,810 So in our case, within the same Scotland file, so it's available for our main function, but also 72 00:05:03,810 --> 00:05:06,000 for our my function function. 73 00:05:06,840 --> 00:05:07,230 OK. 74 00:05:07,650 --> 00:05:14,130 So this is the whole idea of scope, and I hope this will make things a lot clearer as we move on.