Home Web Front-end Front-end Q&A 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)

Jan 19, 2022 pm 05:53 PM
html javascript front end

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;
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles