Definition of function (Declaration of function)
grammar
function Function name(Parameter 1,Parameter 2,...){ //The statement that the function will execute }//This is called a function declaration //It can also be written like this var f = function(Parameter 1,...){...}//This is also called function expression. //In this way, f is the function name, function (parameter 1,...) {...} Also known as anonymous functions //A function expression can also provide a function name and can be used to refer to itself within a function or to identify the function in the debugger stack trace: //For example, a function that implements the hierarchy of n const factorial = function fac(n) {return n<2 ? 1 : n*fac(n-1)}; console.log(factorial(3)); //You can also use the Function constructor to create a Function const sum = new Function('a', 'b', 'return a + b'); console.log(sum(2, 6));//8
Note: the declaration of the function will be raised.
console.log(square(5));//25 function square(n) { return n*n } //In this way, the function can also be called successfully
However, when using the expression of a function, the function will not be promoted.
console.log(square(5)); //square is not a function var square = function(n) { return n * n; } //Note that there is a difference between declaring variables with var and let and const //Using var will also have variable promotion. console.log(square(5)); //This will output: uncaught referenceerror: cannot access' Square 'before initialization const square = function(n) { return n * n; }
Scope of function
Simply put, the variables defined inside the function can only be used inside the function. The function can use the variables defined by its parent function and any other variables that the parent function has access to. (global variables are accessible to anyone)
recursion
Call yourself inside a function.
In fact, recursive functions use stacks: function stacks.
This stack like behavior can be seen in the following example:
function foo(i) { if (i < 0) return; console.log('begin:' + i); foo(i - 1); console.log('end:' + i); } foo(3); // Output: // begin:3 // begin:2 // begin:1 // begin:0 // end:0 // end:1 // end:2 // end:3
Analysis: Note: the stack is first in and last out.
When the foo(3) function is executed for the first time, put foo(3) into the stack. At this time, if no other function is executed, run foo(3) and output begin:3. Then find a function to call, and put foo(3-1) into the stack. At this time, foo(2) is the top of the stack, execute it first, and then output begin:2. Then find a function to be executed, and put this function foo(2-1) into the stack, At this time, foo(1) is the uppermost function in the stack. Execute it first, and then output begin:1,... And so on until foo(0-1) meets the judgment condition I < 0. return,foo(-1) function is completed, comes out of the stack, and then executes the uppermost function in the current stack, that is, foo(0). At this time, foo(0) has been executed to console log(‘end:’ + i); This code is, so it directly outputs end:0, and then the function is executed and out of the stack. It continues to execute the top function in the current stack until the last function is out of the stack.
closure
A function is bound with a reference to its surrounding state (lexical environment) (or the function is surrounded by a reference). Such a combination is a closure. That is, a closure allows you to access the scope of its outer function in an inner function.
Use of closures:
function fu(){ var name = 666; function zi(){ alert(name); } return zi; } var f = fu(); f(); //According to the analysis, after the fu() function is executed, it returns a function Zi, that is, it is equivalent to assigning the function Zi to F, and then executing the function through f(), that is, it is equivalent to executing zi(),
Simply put, a function that has access to a variable in the scope of another function is a closure.
function fu(){ var num = 1; function zi(){ num++; console.log(num); } return zi; } var f = fu(); f();//2 f();//3 var a = 1; function add(){ a++; console.log(a); } add();//2 add();//3 //By comparing the above, we can find that they are almost the same. Function add is defined in the global, and variable a is also defined in the global. In fact, it is in the same scope, and function zi is defined in function fu, so function zi can call all variables that function fu can call. However, the function add is not a closure function because it uses variables that are not in the scope of another function. //But note that the above function add is not a closure. Because the outer layer of the function add is not a function.
Note: you can nest another function inside one function. A nested (inner) function is private to its container (outer) function. It also forms a closure. A closure is an expression (usually a function) that can have its own independent environment and variables.
Since a nested function is a closure, it means that a nested function can "inherit" the parameters and variables of the container function. In other words, the inner function contains the scope of the outer function.
It can be summarized as follows:
- Internal functions can only be accessed in external functions (only parent access).
(to put it bluntly, in fact, you must return or call it directly in an external function)
function fu(){ var num = 1; function zi(){ num++; console.log(num); } return zi; } var f = fu(); f();//2 f();//3 function fu2(){ var a= 1; function zi(){ a++; console.log(a) } zi(); } fu2(); //These are closures, and they must access the internal function zi through the external functions Fu and fu2.
- The inner function forms a closure: it can access the parameters and variables of the outer function, but the outer function cannot use its parameters and variables.
Advantages and disadvantages of closures
advantage:
1. Protect the variables in the function and strengthen the encapsulation
2. Maintain a variable in memory (using too much will become a disadvantage and occupy memory)
The disadvantage of closures is resident memory, which will increase memory usage. Improper use can easily lead to memory leakage.