Date: February 4
1. Higher order function
What is a Function: a Function is a data type [Function] Function that can be defined by an expression
Higher order function: since a function is of a type and can be used as a parameter or return value of another function, the function is called a higher-order function (simply a function calling function)
Case:
// The declarative myFunction02 of the function indicates that the name of the function indicates the function itself function myFunction02() { console.log("xxxx"); } console.log(myFunction02); // Print function itself myFunction02() // Function call execution console.log("------------------------------"); function fn() { console.log("I'm a function and I want to be an argument to another function"); } /** * a Common parameters * cb function * * fn02 Is a higher-order function with another function as its parameter */ function fn02(a,cb) { // console.log(cb); console.log(a); cb() } fn02(1,fn) // 1 console.log("------------------------------"); // Function can be used as the return value of another function /* Requirements: Find 10 + 100 10 + 1000 10+ 10000 10+n */ function getSum(a) { // The latter function is the return value of the previous getSum function return function(b) { return a + b; } } var callback = getSum(10) /* callback = function(b) { return 10 + b; } */ var r1 = callback(100) var r2 = callback(1000) console.log(r1); // 110 console.log(r2); // 1010
2. Anonymous function
1. Anonymous function: anonymous function [execute function immediately]: a function without a name
2. Features: it can only be used once, and [execute now] needs to be called immediately
Case:
// Anonymous function with no parameters and no return value (function() { console.log("xxxxx"); })() console.log("---------------------"); // Anonymous function with parameters and no return value (function(a,b) { console.log(a,b,"######"); })(1,2) // Anonymous function with parameter and return value var result = (function(a,b) { return a + b })(1,2) console.log(result);
3. Scope, closure
1. Scope: the scope within which a variable can work
2. Global variable: a variable that can be accessed anywhere
3. Local variable: a variable that can be accessed only within a fixed code fragment
4. Scope:
Block level scope: es5 has no block level scope, {}; if() {} ; for() {} ; while() {}
Function scope: the only scope in es5. Because there is a function scope in es5, the variables in the function are local variables
5. Scope chain: a chain structure inside a function that points outside the function. When the internal scope accesses the external scope, the scope chain will be generated [this access method is called closure]
6. Closure: the internal scope accesses the external scope, and the law of accessing variables: the proximity principle
7. Function of closure: protect the current variable from global pollution, which can extend the scope of local variables [reflected in the high-order function whose return value is a function]
Case:
function myFunc() { // The current variable b can only be used in the current function, so it cannot be accessed outside the function // var means that local variables in a function are defined var b = 20; // If var is removed, then this variable is a global variable c = 100; console.log(b); // 10 the printed value can access the variable b in the current function } myFunc() //console.log(b); console.log(c); console.log("-----------------------"); function func01() { var a = 10; func02(); function func02() { // The internal scope has this variable, and the external scope also has the proximity principle that this variable has priority to access the internal scope var a = 20; console.log(a); // Internal scope accessed external scope } } func01()
Case: (closure)
/* 1. Traversal 1-4 2. All values are + 5 and printed 6 7 8 9 */ function fn() { for(var i = 1;i<=4;i++) { (function(a) { a+=5 console.log(a); })(i) } // p(i) using ordinary function calls // function p(a) { // a+=5 // console.log(a); // } } fn()
4. Characteristics of variables in scope
1. Variable
1.1 the first common variable is var a = 10
1.2 the second {function a() {}
2. Characteristics of variables in action:
2.1 the function can be automatically promoted to the front of the current scope
2.2 the variable name of a common variable can be automatically promoted to the front of the current scope, but the value cannot be changed
Case:
// Interview questions var a = 10; function fn() { // var a; console.log(a); var a = 20; } fn(); //undefined function fn2(a) { console.log(a); var a = 20; } fn2(30)