The closure principle Analysis _javascript technique in JavaScript

Source: Internet
Author: User
Tags closure
Let's look at a definition:
Closure
The term "closure" refers to an expression (usually a function) that has many variables and an environment that binds them, and therefore these variables are also part of the expression.
This shows that the closure in JavaScript is a function that contains the context, that is, the function of the role is based on its environment, which is not beyond, and linear algebra is not a little déjà vu it?
In other words, the closure is designed to achieve OO. In JavaScript, there is no public, private, protect attribute identity like C + +, and it is difficult to build a class. "Class is data with behavior, and closure is behavior with data", in JavaScript we use function definitions instead of class definitions, and closures instead of setter/getter methods. Take a look at a livecode:
Copy Code code as follows:

Function F1 () {
var n=1;
Function getter () {
alert (n);
}
return getter;
}

The Declaration and function getter of n above form a typical closure. The final return function, the "behavior" just described, is designed to get the value of N, so closures are the behavior of data.
In addition, I think Nanyi's closure is also very concise: "My understanding is that closures are functions that can read internal variables of other functions." ”
Another more scholarly explanation:
Http://demo.jb51.net/js/javascript_bibao/index.htm
I hope that you can really understand the closure in terms of the academic definition, because all interpretation and simplification of the definition of closures is a one-sided interpretation of JavaScript.
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.