What does javascript closure mean?

青灯夜游
Release: 2021-11-24 13:48:55
Original
6361 people have browsed it

In JavaScript, a closure refers to a function; when two functions are nested within each other, the inner function is a closure. A typical closure body is a function with a nested structure; the inner function refers to the private members of the outer function, and at the same time the inner function is referenced by the outside world. When the outer function is called, a closure is formed.

What does javascript closure mean?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is a closure

The so-called closure refers to a function. When two functions are nested within each other, the inner function is a closure.

Formation principle

When the function is called, a temporary context active object will be generated. It is the top-level object of the function scope. All private methods, variables, parameters, private functions, etc. in the scope will exist as attributes of the context active object.

After the function is called, by default the context active object will be released immediately to avoid occupying system resources. However, if the private variables, parameters, private functions, etc. within the function are referenced by the outside world, the context active object will continue to exist temporarily until all external references are cancelled.

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

According to the scope chain, the inner function can access the private members of the outer function. If the inner function references the private members of the outer function, and the inner function is passed to the outside world, or is open to the outside world, then a closure body is formed. This external function is a closure body. After it is called, the active object will not be canceled temporarily, and its properties will continue to exist. The private members of the external function can be continuously read and written through the internal function.

Closure structure

A typical closure body is a function of a nested structure. The inner function references the private members of the outer function, and at the same time the inner function is referenced by the outside world. When the outer 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) {  //外部函数
    return function (y) {  //内部函数,通过返回内部函数,实现外部引用
        return x + y;  //访问外部函数的参数
    };
}
var c = f(5);  //调用外部函数,获取引用内部函数
console.log(c(6));  //调用内部函数,原外部函数的参数继续存在
Copy after login

The parsing process is briefly described as follows:

  • During the JavaScript script pre-compilation period, the declared function f and variable c are first lexically pre-parsed.

  • During JavaScript execution, call function f and pass in the value 5.

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

  • The value of parameter x is 5, which is mapped to the x property of the active object.

  • The inner function references parameter x through the scope chain, but has not yet been executed.

  • After the external function is called, the internal function is returned, causing the internal function to be referenced by the external variable c.

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

  • When c is called, that is, when the internal function is called, 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 closure is to facilitate the storage of data during expression operation. However, its shortcomings cannot be ignored:

  • After the function is called, the calling object cannot be logged out, which will occupy system resources. Extensive use of closures in scripts can easily cause memory problems. leakage. Solution: Use closures with caution and don’t abuse them.

  • Due to the role of closures, the values ​​they save are dynamic, and exceptions or errors may easily occur if not handled properly.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What does javascript closure mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template