1: Scope chain:
– The scope chain is stored in an implicit property [[scope]], which is inaccessible but exists. It is accessed by the JS engine, which stores the scope chain AO GO. It can also be said that the set of AO and GO
Scope chain details
var global
function a() {
// When you enter a, you can see a's own AO, and then you can see the global go
function b() {
// Select * from a where b's ao is located and a's AO is located
var bb = 123;
aa = 0
}
var aa = 234
b()
}
a() // The link between A's AO and B's AO and global go is broken
Copy the code
Closure underlying principles
// Closure: Internal functions are saved externally
function a() {
var aa = 123
function b() {
var bb = 234
console.log(aa);
}
return b
}
var res = a()
res()
Copy the code
After the execution of A, a is disconnected from the chain of scope, but B is not disconnected from the chain of scope at this time, he can still execute, you can see the AO of function A. Because when function B is born, function A’s AO is taken by b
This is the basic principle of scope chains and closures, this article, a little attention should be able to understand. Feel free to discuss in the comments if you don’t know. Check out the next article for more on closures