Easy Tutorial
❮ Android Tutorial Exercise 4 Verilog2 File ❯

What is a closure? Advantages and disadvantages of closures?

Category Programming Technology

Closure is a challenging aspect of JavaScript and also one of its features. Many advanced applications rely on closures to function.

1. Variable Scope

To understand closures, one must first understand JavaScript's unique variable scope.

Variable scope can be either global or local.

What makes JavaScript special is that functions can directly access global variables, but cannot access local variables from outside the function.

Note: When declaring a variable inside a function, always use the var command. If not used, you are actually declaring a global variable!

2. How to access local variables from outside a function?

For various reasons, we sometimes need to access local variables inside a function. However, as mentioned, this is normally impossible! It can only be achieved through a workaround.

That is, define another function inside the function.

function f1(){
    var n = 999;
    function f2(){
        alert(n); // 999
    }
}

In the above code, function f2 is nested inside function f1, making all local variables of f1 visible to f2. However, the reverse is not true; local variables inside f2 are not visible to f1.

This is JavaScript's unique "chain scope" structure, where child objects can look up variables from parent objects layer by layer. Therefore, all variables of the parent object are visible to the child object, but not vice versa.

Since f2 can access local variables of f1, by returning f2, we can access f1's local variables from outside f1!

3. Concept of Closure

The f2 function in the above code is a closure.

Closure definitions in various professional literature are very abstract. My understanding is: a closure is a function that can access variables inside another function.

In JavaScript, only a sub-function inside a function can access its local variables. Therefore, a closure can be simply understood as a "function defined inside another function".

Essentially, a closure is a bridge connecting the inside and outside of a function.

4. Uses of Closure

Closures can be used in many ways. Their main uses are: one, to access variables inside a function (as mentioned earlier), and two, to keep these variable values in memory, preventing them from being automatically cleared after f1 is called.

Why is this so? The reason is that f1 is the parent function of f2, and f2 is assigned to a global variable, which keeps f2 in memory. Since f2 depends on f1, f1 also remains in memory and is not reclaimed by the garbage collector after the call.

Another noteworthy point in this code is the line "nAdd=function(){n+=1}". First, nAdd is not preceded by the var keyword, making it a global variable rather than a local one. Second, nAdd's value is an anonymous function, which itself is a closure, so nAdd acts as a setter, allowing operations on local variables of the function from outside.

5. Points to note when using closures

>

Original link: https://www.cnblogs.com/cxying93/p/6103375.html

** Share My Notes

Cancel

-

-

-

❮ Android Tutorial Exercise 4 Verilog2 File ❯