What is a closure package?

Source: Internet
Author: User

# Closed Bag

Closures are meant to be closed and wrapped up. In simple terms, a structure with a closed function and a package function. The so-called closures are,
There is a closed, non-public, parcel structure, or space.

In JS, functions can form closures. The general function is a closed structure of the code structure, that is, the characteristics of the package, and according to the scope rules,
Only allow the function to access external data, external access to the internal function of the data, that is, closed external non-public characteristics. Therefore, functions can form closures.

# # Closure to solve what problem

1. Closures do not allow outside access
2. The problem to be solved is to access the data indirectly

Functions can form closures, the problem is to access the data inside the function

```
function foo () {
var num = 123;
return num;
}
var res = foo ();
Console.log (RES); = 123
```
1. It is true that the data in the function is accessed
2. However, the data cannot be accessed for a second time. Because another call to Foo at the second visit means that there is a new num = 123 coming out.

The data within the function cannot be accessed directly outside of the function, so if a function is defined within the function, then the intrinsic function is directly accessible.
```
function foo () {
var num = Math.random ();
function func () {
return num;
}
return func;
}
var f = foo ();
F can access this num directly
var res1 = f ();
var res2 = f ();
```

What is a closure package?

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.