Closures and exposed interfaces---learning notes

Source: Internet
Author: User

Closure: A function can access the context in which it was created. ---The essence of JavaScript language

Production principle: There is no block-level scope in JavaScript, only function scope, parameters and variables defined anywhere in the function are visible anywhere within the function, not visible outside the function.

How to use: You can implement the privatization of variables by creating local variables inside the function. (External not visible)

Note: An intrinsic function accesses an external function's variable when the actual variable is accessed (value is the value of the variable when the external function returns).

Knowledge Point: ① variables in an external function are not destroyed after an external function returns as long as the intrinsic function requires it. (more waste of memory)

② through the definition of internal functions to implement internal variable information query and modification, to achieve interface exposure.

As an example:

 var  myObject = (function   () { var  value=0;  return   {getValue:  function   () { return         Span style= "COLOR: #000000" > value; }, SetValue:  function   (v) {if  (typeof  v = = = ' Number ' " {value  = V;        }        }    };    }()); 

This code obtains a MyObject object, contains two methods, only these two methods can get or modify the value variable, there is no other way to access the value variable.
This value variable is called a private variable , and the two common methods that can access private variables are called privileged methods .

Let me give you an example:

 //  Constructs a function whose original meaning is to return an array of functions through an intrinsic function, each of which is a function The 
//Array entry function returns the current array index // But the last function of each item will be returned to var eg2 = function () { var I, Result = new Array (); for (i=0; i<10; i + = 1) {Result[i] = function () { return I; }} return result;};

Why does the function of each item in the last array return 10? Because the internal anonymous function accesses the variable i instead of the value of I when the function was created, the value of the variable i is saved in 10 after the For loop executes, and the value of the variable I returned in the function of the result array returned by the last external function is 10.

Can be modified to solve this:

//creates a helper function outside of the for loop, returning a function that returns the value passed invarEG2 =function(){    varresult =NewArray (); varHelper =function(e) {return function(){            returne;    }    }; vari;  for(i=0; i<10; i + = 1) {Result[i]=Helper (i); }    returnresult;};

Each time the helper () function is called in the For Loop, passing in the variable I, because the function argument is passed by value , helper () copies the value of the current variable i to the local variable e, and returns a function that returns the variable E.

The variable e here is a local variable for each array-item function, and each holds its own distinct value.

Closures and exposed interfaces---learning notes

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.