The closure in JavaScript o__o "...

Source: Internet
Author: User
Tags closure

One, closure of the package!?

  Closures (closure) are a difficult part of the JavaScript language and are not easy for beginners to understand, so let's take a look at the implications of closures.

Baidu Encyclopedia and "official" explanation: the so-called "closure", refers to a lot of variables and the environment that binds these variables (usually a function), so these variables are also part of the expression.

Wikipedia: in programming languages, closures (also known as lexical closures or function closures) are techniques for implementing lexical scope name bindings in languages with first-class functions.

However, we see that these official interpretations do not clearly understand what a closure is, but a question mark, "What are you talking about?" ”。

In fact, a closure is simply a function that can read local variables defined inside a function, whereas in JavaScript only a child function inside a function can read a local variable, we can also interpret the closure as a function defined inside the function.

Second, the function of the variable

To understand closures, let's first understand the variables in the function, which have their own scopes. What is a scope? Now, generally speaking, the name used in a program code is not always valid/available, and the scope of the code that qualifies the name for the availability is the name.

There are two types of variables, one is global, the other is local, which is what we call global variables and local variables.  

var a=50; function fun1 () {    alert (a);} Fun ();

In the above code, a is defined outside the function, so calling variable a inside the function can naturally read its value. Here's a we'll call it global variables.

function fun2 () {    var a=50;} alert (a);

In this code, we define a within the function, then call a outside the function, it will error, tell you "A is not defined", we can not find a ah? Have we defined a? Of course it's defined! It's just a local variable.

Third, how to find the "hidden" in the function of the variable

When we hit the keypad code, there are always some reasons, we need to get the local variables inside the function, then "hide" in the function of the local variables we need a way to extract him.

That is, inside the function, we define a function.

function Fun3 () {    var a=50;     function Fun4 () {        returnA;    }     return fun4 ();}
Console.log (a); Error Console.log (FUN3 ());

In the above code, we define a variable A in the FUN3, so that we in the function fun3 out with a also error can not find it, and for the function fun3 internal function fun4, A is callable, because outside a in the function fun4 outside, In turn FUN4 if a variable is defined then FUN3 is not callable.

Another concept that is involved here is the chain scope, which is what we call the scope chain. Scope chain is the same variable name will produce a scope chain, access to the variable name, the first to find out whether the scope of this layer contains the variable, if not to go to the parent scope to find, eventually find the global variable, if the global variable can not find the variable, then will be an error.

So if you want to call the variable a outside the function fun3, we need to define a function inside the function Fun3 fun4, let it return to the variable A that he can call, then the result of the function fun4 is the variable A, then we return the function Fun4, then we execute the function fun3, The resulting result is the value of the variable A.

Simply put, we can read its internal variables outside the FUN3, just by taking Fun4 as the return value.

Iv. what to pay attention to when calling local variables (using closures)?

1. Closures will allow variables inside the function to be stored in memory, and the memory consumption is very large, so the closure cannot be abused, otherwise the performance of the page will be degraded.

2. Do not arbitrarily change the value of the internal variables of the parent function.

Finally, the closure is the ability to read other functions of the internal variables of the function, can let us in the parent function out of the parent function of the variable, there is code to understand the situation is not difficult, it is important in the unpredictable code, closure of the specific application or need we really understand it to be proficient in the application.

    

The closure in JavaScript o__o "...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.