Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does javascript mean by closure?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is a closure in javascript", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is a closure in javascript".

In javascript, a closure is a function; when two functions are nested within each other, the internal function is the closure. A typical closure is a nested function; the internal function refers to the private members of the external function, while the internal function is referenced by the outside. When the external function is called, the closure is formed.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

What is closure?

The so-called closure means a function. When two functions are nested within each other, the internal function is the closure.

Formation principle

When a function is called, a temporary context active object is generated. It is the top-level object in the scope of the function, and all private methods in the scope, such as variables, parameters, private functions, and so on, will exist as attributes of the context active object.

After the function is called, by default, the context active object is released immediately to avoid taking up system resources. However, if private variables, parameters, private functions, and so on within the function are referenced by the outside world, the contextual active object will continue to exist temporarily until all external references are logged out.

However, the scope of the function is closed and inaccessible to the outside world. So under what circumstances can private members within a function be accessed by the outside world?

According to the scope chain, internal functions can access private members of external functions. If the internal function refers to the private member of the external function, and the internal function is passed to the outside world, or open to the outside world, then the closure is formed. This external function is a closure, after it is called, the active object will not be logged out temporarily, its properties will continue to exist, and the private members of the external function can be read and written continuously through the internal function.

Closure structure

A typical closure is a function of a nested structure. The internal function refers to the private members of the external function, while the internal function is referenced by the outside. When the external function is called, a closure is formed. This function is also called a closure function.

The following is a typical closure structure.

Function f (x) {/ / external function return function (y) {/ / internal function, by returning the internal function, the external reference return x + y; / / access the parameters of the external function};} var c = f (5); / / call the external function to get a reference to the internal function console.log (c (6)); / / call the internal function, the parameters of the original external function continue to exist

The parsing process is briefly described as follows:

During the pre-compilation period of the JavaScript script, the declared function f and variable c are first preparsed by lexical parsing.

During JavaScript execution, the function f is called and the value 5 is passed in.

When parsing function f, the execution environment (function scope) and the active object are created, and parameters and private variables and internal functions are mapped to the properties of the active object.

The parameter x has a value of 5, which maps to the x attribute of the active object.

The internal function refers to the parameter x through the scope chain, but it has not been executed yet.

After the external function is called, it returns the internal function, which causes the internal function to be referenced by the external variable c.

The JavaScript parser detects that the property of the active object of the external function is referenced by the outside world and cannot unregister the active object, so it continues to exist in memory.

When you call c, that is, the internal function, you can see that the value stored in the parameter x of the external function continues to exist. In this way, subsequent operations can be implemented and x+y=5=6=11 is returned.

The value of closures is to facilitate the storage of data during expression operations. However, its shortcomings can not be ignored:

Because the calling object cannot be logged out after the function call, it will take up system resources, and a large number of closures are used in the script, which can easily lead to memory leakage. Solution: use closures carefully and don't abuse them.

Because of the function of closure, its saved value is dynamic, and it is prone to exceptions or errors if it is not handled properly.

The above is all the content of the article "what does closure in javascript"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report