JS Closed Packet Understanding

Source: Internet
Author: User

Closure is a major feature of JS, but also a major difficulty. Simply put, a closure means that a function can access variables in the outer scope of its function.

The three main features of closures are:

1. Function Nesting function

2. Internal functions can access variables of external functions

3, parameters and variables will not be recycled.

For example:

    1. function Test () {
    2. var a=1;
    3. return function () {
    4. alert (a);
    5. }
    6. }
    7. var try=test ();
    8. try (); //Eject the value of a

In this example, the variable A is inaccessible outside of the test method, but inside the test method, an anonymous function is nested, returned by return, variable A in the test scope,

Can be accessed in anonymous functions. And when the test method executes, the memory that the variable a occupies does not release, to achieve the purpose that the nested function can also be accessed.

The function of closures is that private variables and methods can be designed by closing the packet.

For example: Create a Perosn class in Java that contains the private variable name.

  1. public class person{
  2. private String name=' WY ';
  3. Public person (val) {
  4. Name=val;
  5. }
  6. Public void setName(val) {
  7. Name=val;
  8. }
  9. Public String getName() {
  10. return name;
  11. }
  12. }


The ability to create a class similar to Java is implemented in JS:

  1. (function () {
  2. var name="Wangyu";
  3. person=function (val) {
  4. Name=val;
  5. }
  6. Person.prototype.setname=function (val) {
  7. Name=val;
  8. }
  9. Person.prototype.getname=Function () {
  10. return name;
  11. }
  12. })();
  13. var person1=new Person ("SJ");
  14. Alert (this.name)//undefined because it cannot be accessed outside the function domain
  15. Alert (Person1.getname ()); //wangyu

The name inside the function is not accessible externally because it is in the function scope, but can be achieved by creating a person object, calling the person's method.

The purpose of modifying and accessing the name value is similar to a private variable in a Java class that cannot be accessed externally and can only be accessed through a class method.

Let's look at an example of a private variable:

  1. var aaa = (function () {
  2. var a = 1;
  3. function bbb () {
  4. a++;
  5. alert (a);
  6. }
  7. function CCC () {
  8. a++;
  9. alert (a);
  10. }
  11. return {
  12. b:bbb, //json structure
  13. C:ccc
  14. }
  15. })();
  16. Alert (AAA.A)//undefined
  17. AAA.B (); //2
  18. AAA.C () //3


Summarize:

1. Closures are functions that have access to variables in another function scope, and the most common way to create closures is to create another function within one function and access the local variables of the function through another function. The disadvantage of closures is that resident memory increases memory usage and is prone to memory leaks if used improperly.

2, do not have to struggle in the end how to calculate the closure, in fact, you write every function counts as closures, even if the global function, you access the function outside the global variables, is the embodiment of the closure.

JS Closed Packet Understanding

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.