preface

If you’re learning JavaScript, you probably know that external space can’t access internal variables. We tend to just know this basic rule. What’s the basic underlying principle that implements this basic rule? Today I will take you to understand the scope chain from the perspective of small white, I hope to give you some help!

scope

1. What is scope

In simple terms, scope (English: scope) is, according to a set of rules to find the variable name scope can be easy to understand for a closed space, this space is closed, not external influence, the outer space cannot access to the internal space, but the internal space can access to the package, the outer space.

2. [[Scopes]] attribute

In javascript, every function is an object, and there are properties in the object that we can access and properties that we can’t access freely. The [[Scopes]] property is one of them, and can only be read by the javascript engine. In fact, [[scope]] is often referred to as the scope, which stores the context collection of the scope runtime.

Here because func. Prototype. The constructor and func point to the same function, so here we access function func prototype object to see [[Scopes]] attribute

3. Scope chain

[[scope]] is a collection of execution context objects stored in [[scope]. This collection is linked in a chain, which is called a scope chain. JavaScript looks up variables through the scope chain by looking down the top of the scope chain (look up objects in the function within the scope chain)

4. Schematic finding variable principle

// Use the following code as an example to illustrate how JavaScript looks up variables via scope chains **
function a() {
  function b() {
      var b = 234;
  }
  var a = 123;
  b();
}
var glob = 100;
Copy the code

1. When the global function a() is defined, the scope chain is as follows

The [[Scopes]] property of the function points to the scope chain object, which has only one key-value pair. The key-value pair points to the global object, which stores globally accessible objects, the outermost scope, accessible to everyone.

2. When the global function a() is invoked, the scope chain is as follows

The first Activation Object that the scope chain can access is the key-value pair in the Activation Object, and the global Object if not

3. When function B of function A () is defined, the scope chain of b is as follows

When B is defined but not called, the chain of scope for B is the same as for A

4. When function B in function A () is invoked, the scope chain is as follows

The scope chain first points to the Activation Object of function B (), and the lookup variables are also accessed in the scope chain order, stopping when they are found

5. To summarize

Are outside the scope cannot access to the internal scope of reason is outside the scope of the scope chain is not within the scope of the Activation Object, so I can’t access to the internal variables, is in order of their internal scope variable access in accordance with the scope chain, from the inside first lookup, no outward along the scope chain looking, and external is a global scope.