search
HomeWeb Front-endJS Tutorialjavascript closure_javascript skills

For beginners, it is still difficult to understand Javascript closures. The purpose of writing this article is to use the most popular words to reveal the true face of Javascript closures and make it easier for beginners to understand.
1. What is closure?
The "official" explanation is: a closure is an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also part of the expression. I believe that few people can directly understand this sentence because his description is too academic.
In fact, this sentence in layman’s terms means: all functions in JavaScript are closures. But generally speaking, the closure generated by nested functions is more powerful, which is what we call "closure" most of the time. Look at the following code:

Copy code The code is as follows:

function a() {
var i = 0;
function b() {
alert( i);
}
return b;
}
var c = a();
c();

This code has two characteristics:
1. Function b is nested inside function a.
2. Function a returns function b.
In this way, after executing var c=a(), variable c actually points to function b. Variable i is used in b. After executing c(), a window will pop up to display the value of i (first The times are 1). This code actually creates a closure. Why? Because variable c outside function a refers to function b inside function a, that is to say:
When the internal function b of function a is referenced by a variable outside function a, a so-called "closure" is created. Bag".Let's put it a little more clearly. The so-called "closure" is to define another function in the constructor body as the method function of the target object, and the method function of this object in turn refers to the temporary variable in the outer outer function body.
This allows the temporary variable value used by the original constructor body to be indirectly maintained as long as the target object can always maintain its method during the lifetime. Although the initial constructor call has ended and the name of the temporary variable has disappeared, the value of the variable can always be referenced in the method of the target object, and the value can only be accessed through this method.
Even if the same constructor is called again, only new objects and methods will be generated. The new temporary variables only correspond to new values ​​and are independent from the last call. In order to have a deeper understanding of closures, let us continue to explore the functions and effects of closures.
2. What are the functions and effects of closure?
In short, the function of the closure is that after a is executed and returned, the closure prevents Javascript's garbage collection mechanism GC from reclaiming the resources occupied by a because of the execution of a's internal function b. Need to rely on variables in a. This is a very straightforward description of the role of closures. It is not professional or rigorous, but the general meaning is this. Understanding closures requires a step-by-step process.
In the above example, due to the existence of closure, i in a will always exist after function a returns. In this way, every time c() is executed, i will be the value of i that is alerted after adding 1. So let's imagine another situation. If a returns something other than function b, the situation is completely different.
Because after a is executed, b is not returned to the outside world of a, but is only referenced by a. At this time, a will only be referenced by b. Therefore, functions a and b refer to each other but are not disturbed by the outside world (being External reference), functions a and b will be recycled by GC. (The garbage collection mechanism of Javascript will be introduced in detail later)
3. The micro world of closures
If you want to have a deeper understanding of closures and the relationship between function a and nested function b, We need to introduce several other concepts: function execution context (execution context), active object (call object), scope (scope), and scope chain (scope chain). Take the process of function a from definition to execution as an example to illustrate these concepts.
1. When defining function a, the js interpreter will set the scope chain of function a to the "environment" where a is when defining a. If a is a global function, the scope chain will There is only window object.
2. When executing function a, a will enter the corresponding execution context.
3. In the process of creating the execution environment, a scope attribute will first be added to a, that is, the scope of a, and its value is the scope chain in step 1. That is, the scope chain of a.scope=a.
4. Then the execution environment will create a call object. An active object is also an object that has properties, but it does not have a prototype and cannot be accessed directly from JavaScript code. After creating the active object, add the active object to the top of a's scope chain. At this time, a's scope chain contains two objects: a's active object and the window object.
5. The next step is to add an arguments attribute on the active object, which stores the parameters passed when calling function a.
6. Finally, add all formal parameters of function a and references to internal function b to the active object of a. In this step, the definition of function b is completed, so just like step 3, the scope chain of function b is set to the environment where b is defined, that is, the scope of a.
At this point, the steps from definition to execution of the entire function a are completed. At this time, a returns the reference of function b to c, and the scope chain of function b contains a reference to the active object of function a, which means that b can access all variables and functions defined in a. Function b is referenced by c, and function b depends on function a, so function a will not be recycled by GC after returning.
When function b is executed, it will be the same as the above steps. Therefore, the scope chain of b during execution contains 3 objects: b's active object, a's active object and window object. When accessing a variable in function b, the search order is:
1. Search first Its own active object will be returned if it exists. If it does not exist, it will continue to search for the active object of function a until it is found.
2. If function b has a prototype prototype object, it will first search for its own prototype object after searching for its own active object, and then continue to search. This is the variable lookup mechanism in Javascript.
3. If it cannot be found in the entire scope chain, undefined is returned.
Summary, two important words are mentioned in this paragraph: the definition and execution of functions. The article mentions that the scope of a function is determined when the function is defined, not when it is executed (see steps 1 and 3). Use a piece of code to illustrate this problem:
Copy code The code is as follows:

function f(x ) {
var g = function () { return x; }
return g;
}
var h = f(1);
alert(h());

The variable h in this code points to the anonymous function in f (returned by g).
◆ Assuming that the scope of function h is determined by executing alert(h()), then the scope chain of h at this time is: h's active object ->alert's active object->window object.
◆ Assume that the scope of function h is determined when it is defined. That is to say, the anonymous function pointed to by h has its scope determined when it is defined. Then during execution, the scope chain of h is: h's active object ->f's active object->window object.
If the first assumption is true, the output value is undefined; if the second assumption is true, the output value is 1. The running results prove that the second assumption is correct, indicating that the scope of the function is indeed determined when the function is defined.
4. Application scenarios of closures
1. Protect variables within functions. Taking the initial example as an example, i in function a can only be accessed by function b and cannot be accessed through other means, thus protecting the security of i.
2. Maintain a variable in memory. Still as in the previous example, due to closure, i in function a always exists in memory, so every time c() is executed, i will be incremented by 1.
3. Implement JS private properties and private methods (cannot be accessed externally) by protecting the security of variables. Recommended reading: http://javascript.crockford.com/private.html. Private properties and methods cannot be accessed outside the Constructor:
Copy code The code is as follows:

function Constructor(...) {
var that = this;
var membername = value;
function membername(...) {...}
}

The above three points are the most basic application scenarios of closures, and many classic cases originate from them.
5. Javascript’s garbage collection mechanism
In Javascript, if an object is no longer referenced, then the object will be recycled by GC. If two objects refer to each other and are no longer referenced by a third party, then the two objects that refer to each other will also be recycled. Because function a is referenced by b, and b is referenced by c outside a, this is why function a will not be recycled after execution.
6. Conclusion
Understanding JavaScript closures is the only way to become an advanced JS programmer. Only by understanding its interpretation and operation mechanism can you write safer and more elegant code. .
Statement
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
JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)