Scope chain :
Each of the function functions of JavaScript has its own scope, which is saved using active object (AO) active objects, forming a scope chain in the nested functions.
The scope chain is the AO chain from inside to outside
The search for variables:
Variables used in the function fn3, such as those found within the FN3 scope, are looked up to the outer fn2 scope, and so on, until the Global object window
Code Demo:
var c = 5;function T1 () { var d = 6; Function T2 () { var e = 7; var d = 3;//if var d = 3 is declared here, then the function is not looking out for the variable d and the output value is console.log (c+d+e); } T2 ();} T1 ();
After understanding the scope chain of JavaScript, it is better to use external variables with higher frequency in the function, it is best to save the external variables as local variables before doing so, which greatly reduces the time to find the variable through the scope chain.
1. JavaScript Advanced function Scope chain