Before we go back to your classes, I would like to talk about something called scope and scope is an
important concept that you need to understand in order for us to move forward with classes.
So scope is the following.
So let's say I'm creating a function here, and I'm going to call this one my function.
And here I pass a value.
Hey, which is of Typekit?
OK.
So if I now try to assign a value to my variable a it doesn't work because it's not really a variable.
This is.
A perimeter so you cannot assign a value to a perimeter within the function.
You can only has an argument when calling the function.
So here I can, for example, pass five as a.
All right.
So you cannot reassign a value to it.
Now let's say I create a variable called a in a year.
What's happening here now?
Well, this variable A is actually a variable.
A is a variable.
And now here, this a
is a parameter.
Now, there are multiple things that we need to look at.
One thing is that this variable A, as I said, is not the same as this parameter A.
So what I can do is I can say for a is going to be a.
So now this a here, our power meter, whatever value it has, will be passed to our variable called
a, which is this variable here.
And now we can go ahead and.
Code printed, so a is and a now, which of the A's will now be printed?
Will it be the perimeter?
Or will it be the variable?
Well, it will be the variable we can see it if we just assign a value of four here, for example.
So I'm going to assign a value of four to my a variable.
And if I run it, you will see that a is four, even though we past five as the eight to it.
So there we are.
It says a is four.
So now you might wonder why Scotland allow us to declare variable with the same name as a parameter
inside the method.
Well, somebody else have the same question.
And here's an answer that was considered correct.
This is called shattering, and it is useful for decoupling your code from other parts of the system.
It is possible because names are bound to the current scope.
So here we see this key word scope again.
Now you can, of course, pause the video and read through all of this, but I personally like to go
to the recommendations directly, which is number one, your code becomes hard to read as two different
things have the same name, which leads to confusion, which already happened.
So I think it's already confusing what we have there.
And another disadvantages once shut out, you can no longer access the original variable within a scope.
So.
If you look back here, we have no way of accessing this variable anymore, except for if we actually
assign it to our VAR A. and then use it.
But then we could have gotten rid of all this.
The whole thing from the get go.
So this would work already or even without this VAR a equals A..
OK.
So this is just one part of scope.
Another part of scope is the fact that and I'm just going to create a different variable here.
I'm going to call this one be.
Our variable, be that we create here, we can use it easily within our function, right, because it's
within scope.
That's where it was created, so that's where we can use it.
But we cannot use it outside of this function.
So I cannot go ahead and say be is equal to three, for example.
This doesn't work because it doesn't find to be unresolved.
References what it says.
OK, so if I create a local variable in here called be this variable D is going to be a very different
variable than this variable B here.
So they are all only visible within the scope of themselves, which is within the scope of their own
function, or if it's within a class, then within the whole class.
So if I create a variable B here and I want to use it in here.
So let's say I want to assign variable B here and like this here, this is going to work because this
variable is created outside of functions, so it's available within the same file.
So in our case, within the same Scotland file, so it's available for our main function, but also
for our my function function.
OK.
So this is the whole idea of scope, and I hope this will make things a lot clearer as we move on.