[Go into JavaScript] 2. Closure (Reading Notes)

Source: Internet
Author: User

Read chapter 7th of JavaScript advanced programming.

I. What is a closure?

A closure is a function that has the right to access variables in another function scope.

Personal perception:

According to the definition in the book, the closure is a function, which can access the variables in another function scope.

So why does this kind of function have such functions? In fact, it uses the function's scope chain.

 

2. Common methods to create a closure: create another function within a function
function createComparisonFunction(propertyName){  return function(object1,object2){    var value1 = object1[propertyName];    var value2 = object2[propertyName];    if(value1 < value2){      return -1;    }else if(value1 > value2){      return 1;    }else{      return 0;    }  };} 

Pay attention to the red points in this example. The code of an internal function is highlighted in red, and this internal function is an anonymous function.

These two lines of code access the variable propertyName in the external function. Even if this internal function is returned and called elsewhere, it can still access the variable propertyName.

The reason why this variable can be accessed is that the scope chain of this anonymous function contains the scope of createComparisonFunction () (represented by a variable object in the scope chain)

 

3. What is the function of a closure? Why does a closure work?

Speaking of this, we have to think back to the previous knowledge about the scope chain. OnlyA thorough understanding of the scope chain can truly understand what the closure plays and why it works..

The scope chain knowledge has been described in detail in my previous blog. I will not repeat it here. I will only talk about the key points.

First, when a function is called, the system creates an execution environment for the function and builds a Scope chain for the execution environment by copying objects in the [[Scope] attribute of the function. The function activity object is pushed to the forefront of the execution environment scope chain as the variable object.

Visible,When a function is called, two things are created. One is the execution environment of the function, and the other is the corresponding scope chain..

Generally, after the function is executed, the partial activity object is destroyed, and only the global scope (that is, the variable object in the global execution environment) is saved in the memory ).

However, the closure is different. The reason is:

 

  A function defined in another function adds an activity object that contains a function (that is, an external function) to its scope chain.

Return to the example above:

function createComparisonFunction(propertyName){  return function(object1,object2){    var value1 = object1[propertyName];    var value2 = object2[propertyName];    if(value1 < value2){      return -1;    }else if(value1 > value2){      return 1;    }else{      return 0;    }  };} 

It can be seen that the scope chain of the anonymous function defined in the createComparisonFunction () function contains the activity object of the external function createComparisonFunction.

After an anonymous function is returned from createComparisonFunction (), its scope chain is changed:

      

In this case, the anonymous function can access all variables defined in createComparisonFunction.

  More importantly, when the createComparisonFunction () function is executed and an anonymous function is returned, its activity objects will not be destroyed, this is because the scope chain of anonymous functions still references this active object.

That is to say, when the createComparisonFunction () function returns, the scope chain of its execution environment will be destroyed, but its activity object will still be referenced. According to the GC policy of JS, because the active object reference times are not 0, it will remain in the memory until the anonymous function is destroyed, createComparisonFunction () will be destroyed.

 

Iv. Closure and variables

As we know, closures are implemented through the mechanism of scope chain. Therefore, they also have a side effect that deserves our attention:

The closure can only obtain the last value of any variable in the function, because the closure only references the variable object on its scope chain.

If you don't need to repeat the example, just give it a try ~ I have a book, so I am too lazy to think about it.

 

V. Summary

Since the closure will carry the scope of the function that contains it, it will occupy more memory than other functions. Excessive use of the closure may cause excessive memory usage. Pay attention to this.

This JS basic book is really good, and we have to look at this part of closure.

Some applications about closures will be described in the next article.

Mutual encouragement.

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.