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,forelements 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
avariable and also to thefirstfunction. - We got the first local scope which is the code inside the
firstfunction, it means that we can access to the code inside thefirstfunction and to his parent scope which is the global scope. - Inside the
first scopewe call an other functionsecondwhich is defined in the scope of thefirstfunction. We now execute the code inside thesecondfunction. - We execute the
console.logfunction who returns thea,bandcvariable, but we don't have access to theaandbvariable 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.