Home  >  Article  >  Web Front-end  >  Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

WBOY
WBOYforward
2022-01-19 17:53:562066browse

This article brings you knowledge about scope chain in JavaScript. External space cannot access internal variables. We often only know this basic rule. So what is the basic underlying principle to implement this basic rule? Hope it can give you some help!

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

Scope

1. What is scope

Simply put, scope (English: scope) is a set of rules for finding variables based on their names. The scope can be generally understood as a closed space. This space is closed and does not It will have an impact on the outside. The outside space cannot access the inside space, but the inside space can access the outside space that wraps it.

2.[[Scopes]]Attributes

In javascript, each function is an object, and there are some properties in the object that we It can be accessed, but some of them are not freely accessible to us. The [[Scopes]] attribute is one of them. This attribute can only be read by the JavaScript engine.

In fact, [[scope]] is what we often call scope, which stores the context collection of the scope runtime.

Here because func.prototype.constructor and func point to the same function, so here we view the [[Scopes]] attribute by accessing the prototype object of the function func

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

3. Scope chain

#The collection of execution-time context objects stored in [[scope]], this collection is connected in a chain , we call this chain connection a scope chain. JavaScript searches for variables through the scope chain. The search method is to query downwards along the top of the scope chain (in which function the object is found inside, it is searched in the function scope chain)

4. Illustration of the principle of finding variables

//以如下代码为例说明JavaScript通过作用域链查找变量的原理**
function a() {
  function b() {
      var b = 123;
  }
  var a = 123;
  b();
}
var glob = 100;

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

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

The [[Scopes]] attribute of the function points to the scope chain object. At this time, the scope chain has only one key-value pair, and this key-value pair points to the global Object, the global object stores things that can be accessed globally, that is, the outermost scope, which everyone can access.

2. When the global function a() is activated and called, the scope chain is as follows

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

At this time, the first thing that the scope chain can access is the key-value pair in the Activation Object. If not, the global object will be accessed

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

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

When b is only defined but not called, the scope chain of b is the same as a

4. When function b in function a() is activated and called, the scope chain is as follows

Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text)

The scope chain first points to the Activation Object of function b(). The search variables are also accessed in the order of the scope chain. Stop when found

5. Summary

The reason why the external scope cannot access the internal scope is that the scope chain of the external scope does not have the Activation Object of the internal scope, so internal variables cannot be accessed. The order of accessing variables in the internal scope is according to the scope. Chain, first search from the inside, if not, look outward along the scope chain, the outside is the global scope

Related recommendations:javascript learning tutorial

The above is the detailed content of Scope chain, the underlying principle of JavaScript (detailed explanation with pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete