JS-function Summary (2), js function Summary
Anonymous functions and closures
All the functions we have learned so far have a function name passing corresponding parameters through the function name to call the function. However, this time in Javascript, the function can be named anonymous without a name. How do we call anonymous functions?
Method 1: This is also a declaration of a function. var sum = function (x, y) {return x + y;} alert (sum (2, 3 )); // "5" Method 2: anonymous functions can also perform self-executed Alert (function (x, y) {return x + y ;}) (2, 3 ));
Self-execution of anonymous functions can solve some problems of anonymous functions. For example, anonymous functions in loops have certain problems.
Function box () {var arr = []; // use an anonymous function in the loop (var I = 0; I <5; I ++) {arr [I] = function () {return I ;}}return arr ;}var B = box (); // when you call box () Here, the loop has been executed and the return is executed. Therefore, the final result of the return arr is 51, and the internal anonymous function output result is for (var I = 0; I <5; I ++) {alert (B [I] (); // The result is five. Here, when the anonymous function is called, I is already 5, so no ++ is done, so the result is 5}
This problem can be solved through self-execution of anonymous functions.
// Self-executed function box () {var arr = []; for (var I = 0; I <5; I ++) through anonymous Functions) {arr [I] = (function (num) {// save each value to the arr return num;}) (I );} return arr;} var B = box (); for (var I = 0; I <5; I ++) {alert (B [I]); // returns 1 2 3 4 5 in sequence}
The closure is also learned in JS functions. What I understand is that a function is placed in a function to form a closure. The official definition is as follows: a code block in a program language allows a level-1 function to exist and the free variables defined in the level-1 function cannot be released until the level-1 function is released, these unreleased free variables can also be applied to a level-1 function. This section combines the examples in the JS video
Function box () {// declare the box function var age = 100; // age is the local variable returnfunction () in the box {// an anonymous function, in this way, the closure age ++ is formed; // The return age is automatically added to the local variable; // The returned result};} var B = box (); // here, the age initialization value is 100 alert (B (); // 101 alert (B (); // 102 alert (B ()); // 103 // accumulate the age. If no closure is used, the age will be initialized every time the box is called back, and the accumulation cannot be realized.
Box () is the first-level function in which age is the initial value of the free variable is 100. Each time B () is performed, it is the self-execution of the anonymous function, because it forms a closure, therefore, the age will not be released when the function is called for self-execution, so as to accumulate.
Summary
Anonymous functions and closures are often combined in practical applications, for example, the value of an anonymous function in a loop is problematic. You can also use closures to solve a lot of knowledge in JavaScript, in combination with the examples mentioned in the video and the preliminary and simple understanding made by myself in the Post-blog application, the knowledge can be used to understand the knowledge points in the video, and it remains to be further understood.