1. JavaScript advanced function scope chain, javascript Functions
Scope chain:
Each function of JavaScript has its own scope. It is saved using Active Object (AO) activity objects, forming a scope chain in nested functions,
The scope chain is the AO chain from the inside out
Search for variables:
Variables used in the fn3 function, if not found in the fn3 scope, search for the outer fn2 scope, and so on until the Global Object window
Code Demonstration:
Var c = 5; function t1 () {var d = 6; function t2 () {var e = 7; var d = 3; // If the declared var d is 3, the function does not look for the variable d, and the output value is 15 console. log (c + d + e);} t2 ();} t1 ();
After understanding the JavaScript scope chain, you can use frequently-used external variables in the function. It is best to save the external variables as local variables before performing operations, in this way, the time for searching variables through the scope chain is greatly reduced.