Home >Web Front-end >JS Tutorial >What is a js closure? Understanding js closures

What is a js closure? Understanding js closures

不言
不言Original
2018-09-01 15:56:552657browse

What is closure in js? For friends who are exposed to the concept of closure for the first time, I believe that many friends will not understand it. The following article will tell you about How to understand js closure.

Closure (closure) is a difficulty in js, and it is also its characteristic. Many advanced applications rely on closures. So let's first take a look at the concept of js closure.

What is closure?

According to the official explanation: the so-called closure refers to an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also the expression part of the formula.

Looking at this concept alone, I believe many friends will find it difficult to understand. After all, this statement is too "official", so let's talk about the understanding of js closures. method. Let you understand more clearly what js closure is.

Understanding of js closure

Let’s first look at an example:

def foo() {
    var a = 1;
    def bar() {
        a = a + 1;
        alert(a);
    }
    return bar;
}
var closure = foo();  // 这个时候返回的是 bar() 这个函数外加其包上的变量 a;
var closure2 = foo(); // 这里同样生成了另外一个闭包(实例)
closure(); // 2
closure2(); // 2 , 绑定了另外一份变量 a
closure(); // 3

From this example we can see:

For the conventional foo() method, the existence of the variable a inside it should disappear after the foo() method is executed, but the foo() method returns a new square bar(), and this method However, the variable a of the foo() method is accessed (JavaScript accesses the parent property through the Scope Chain), and the existence of the method bar() extends the existence time of the variable a, which is similar to closing the variable a in its own scope. As long as the method bar() has not expired, the variable a will always exist with the method bar(), and the existence of the variable a and the method bar() is called a closure.

From the above examples, can we re-summarize our understanding of js closures?

A closure is a lexical scope created by a function. After the variables created in it are referenced, they can be used freely outside this lexical environment.

Closures are usually used to create internal variables so that these variables cannot be modified at will by the outside and can be operated through specified function interfaces.

Okay, now you have a clearer understanding of the closure of js. If you still don’t understand it, you can go to the javascript video tutorial column on the PHP Chinese website to learn about it.

Related recommendations:

Detailed explanation of the use of closures in js

Understand js closures in one minute_javascript skills

How to understand closures in js

The above is the detailed content of What is a js closure? Understanding js closures. For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more