JavaScript Object-Oriented programming (7) closure

Source: Internet
Author: User
Tags anonymous definition closure garbage collection variables

1. What is closure?

What is a closure? A formal explanation is that the so-called closure refers to an expression (usually a function) of an environment that has many variables and binds them, and therefore these variables are also part of this expression.

Many people do not understand this definition because his academic tastes are too strong--perhaps you like to analyze it literally: first, it's an expression that binds a lot of variables and the environment of those variables. But that doesn't make any sense, and it still doesn't tell us what a closure is.

So, let's look at an example:

function add(a) {
 return function(b) {
  return a + b;
 };
}
var func = add(10);
alert(func(20));

I want to go through the previous description of the function, this example should be very clear understanding. The function in JavaScript is the object, and he can do everything the object can do--we first define a function add, it takes a parameter, this function returns an anonymous function, which takes a parameter and returns the same argument as the parameter of the external function. So when we use it, we assign the anonymous function returned by add to Func, and then call Func, and we return the number of these two numbers.

When we create a function in which a variable inside a function can be referenced outside of the function, we call the creation of a closure. Taste it carefully: this is the definition of the closure.

Look at our code: first, it has an internal variable, which is the anonymous function; second, this function returns the anonymous function so that the outside variable can refer to the internally defined variable.

2. The function of closure

What's the use of closures? Maybe not now, so look at this code:

function inc(a) {
 var i = 0;
 return function() {
  return i;
 };
}
var num = inc();
alert(num());

Originally, this variable i is not accessible outside the function, because it is defined by Var, once out of scope, the variable is garbage collected, but, because we use the closure, the outside is able to access the variable, so it is not garbage collection!

If you still don't understand the closure, look at a piece of code that should be familiar:

function Person() {
 var id;
 this.getId = function() {
  return id;
 }
 this.setId = function(newId) {
  id = newId;
 }
}
var p = new Person();
p.setId(1000);
alert(p.getId()); // 1000
alert(p.id); // undefined

We define a class person, which has an id attribute. Now this property behaves like a private variable--it can only be accessed through the setter and getter functions. Yes, that's one use of closures: the private variables of the manufacturing Class!

Closures also have a role in maintaining a variable in memory and not letting the garbage collector recycle the variable. The example here is no longer cited.

Here we simply talk about the concept of javascript closures, and there is no closure of the memory model and so on. This is a fairly important concept, and some members of the Java community have been dreaming about closures, and C # has added the concept of closures in the latest version, except where they are called lambda expressions.

Source: http://devbean.blog.51cto.com/448512/174927

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.