Scope chain
What is scope chain ? Scoping answer the question "Where you can access a certain variable ?" In javascript each new function create a new scope instead of some others programming languages like PHP, >JAVA etc.. where even an
if
,for
elements create a new scope. A scope is determined where the function is written. Let's see some examples to really understand how scope works.
First example
let a = "hello";
first();
function first() {
let b = "hi !";
second();
function second() {
let c = "hey !";
console.log( a + b + c);
}
}
- Like the global execution context there is a global scope which is the scope by default in our example the global scope has access to the
a
variable and also to thefirst
function. - We got the first local scope which is the code inside the
first
function, it means that we can access to the code inside thefirst
function and to his parent scope which is the global scope. - Inside the
first scope
we call an other functionsecond
which is defined in the scope of thefirst
function. We now execute the code inside thesecond
function. - We execute the
console.log
function who returns thea
,b
andc
variable, but we don't have access to thea
andb
variable so the computer will find in the parent scope if it can find the two variables. - The scope chain works in descending order, you can access variable of a parent scope and not the opposite.
Current errors
This is a list of the main errors that you can meet when you use scope chain:
Note: I'm not a wizard there is maybe some issue that you notice above so fell free to open an issue in the github repo if you find a new error not mentioned above.