Today, Lunar New Year, I wish you all the small partners of the dog-like, ah, idle come also nothing, can only delve into their own mightily, that is, as a front-end code of the agricultural identity, must always keep learning attitude, warm so know new, every day to their own a small goal to complete, over the years, to achieve the state, will have a
JS in the closure, presumably, do the Web development program apes have a certain understanding of it, not only JS has this feature, and the weak language type such as Python is also a closure of such a powerful feature, for the old drivers, closures can really be a good thing, But for beginners, closures are a hard-to-understand trait, and in many cases they can be used, but they do not have a deep understanding of what they mean, and sometimes problems arise, and they do not immediately identify the problem. Let's talk about the closure related knowledge in JS.
One, what is closures?
When an intrinsic function is referenced by a variable outside its external function, a closure is formed.
In simple terms, the so-called closure is a closed, non-public, parcel structure or space.
Second, why can functions constitute closures?
Closures are a structure with enclosing and wrapping functions, which are designed to implement functions with private access space. Functions can form closures. Functions are not accessible outside the data functions defined inside the function, that is, the function is closed; The function can encapsulate the code as it is wrapped, so the function can form a closure
Iii. What is the use of closures (characteristics)
The function of a closure is to save its own private variable, which is used externally by the provided interface (method), but cannot be accessed directly by the outside.
When we need to define some variables in the module and want them to remain in memory but not "pollute" the global variables, we can use closures to define the module.
The disadvantage of closures: The disadvantage of closures is that resident memory, will increase memory usage, improper use can easily cause memory leaks.
is the function set function a closure? : No! , a closure is formed when an intrinsic function is referenced by a variable outside its external function.
Four, the basic model of closure
1. Object mode
The function internally defines an object that binds multiple functions (methods), returns an object, and accesses the data within the function using the object's method
functionCreateperson () {var__name__ = ""; return{getName:function () { return__name__; }, SetName:function(value) {//If you do not have the surname Zhang, error if(Value.charat (0) = = = ' Zhang ') {__name__=value; } Else { Throw NewError (' wrong surname, cannot name ' ); } } }}varp =Createperson ();p. Set_name (' Zhang San Fung '); Console.log (P.get_name ());p. Set_name (' Zhang Wangfu '); Console.log (P.get_name ());
2. Function mode
The function internally defines a new function, returns a new function, and uses the new function to obtain the data within the function.
function foo () { var num = math.random (); function func () { return mun; } return func;} var f = foo (); // f can access this num directly var res1 = f (); var res2 = f ();
3. Sandbox mode
Sandbox mode is a self-invoking function that executes in the same way as code written in a function, but does not have any effect on the outside world, such as jquery
(function () { varfunction// all Algorithms} // ..... Jquery.each = function () {} window.jquery = window.$ = jQuery;}) (); $.each (...)
Five, closure of the shortcomings
1, because the closure will make the function of the variables are stored in memory, on the one hand will make memory consumption, on the other hand, the memory leak in IE, the solution, in the Exit function, the unused local variables are all deleted (null off)
2. The closure will change the value of internal variables inside the parent function outside of the parent, so if you use the parent function as an object, the closure as its common method, and the internal variable as its private property, then you need to be careful not to change the value of the function arbitrarily, otherwise you will regret it.
Vi. Summary
For the closure of the ingenious, has always been required through the accumulation of project experience, in order to get a deeper understanding each time, want to become an excellent front-end engineer, not only know it, but also to know why, to make themselves become more excellent.
Talking about the closure in JS