js——higher-order functions, closures, recursion

Table of contents

1. Higher-order functions

1. What is a higher-order function

2. Take a function as a parameter

3. return also returns a function

Two, closure

1. What is a closure

2. The scope of variables

3. Case

4. Result display:

 5. Summary:

3. Recursion

1. What is recursion

2. Case 1

3. Analysis

4. Problem

5. What is stack overflow?

6. Solution

7. Case 2

8. Analysis

9. Result display:

10. Summary


1. Higher-order functions

1. What is a higher-order function

Higher-order functions are those that operate on other functions. In the simplest terms, a higher-order function is a function that takes a function as an argument or returns a value . A function that satisfies the following two functions can be called a higher-order function

(1) Take a function as a parameter

(2) return also returns a function

2. Take a function as a parameter

for example:

<script>
        function fn(a,b,callback){
            console.log(a+b);
            callback && callback() 
//如果第一值callback不存在(false),直接返回false,如果第一个值存在,则运行第二个值 callback()
        }
        fn(1,2,function(){
            console.log('我是最后调用的');
        })
    </script>

The above example means that I defined a function fn, which has three parameters, a, b and a function callback, and then outputs a+b, and then I execute the callback function, it will execute a+b first, and then execute callback function

Screenshot of the result:

3. return also returns a function

for example:

<script>
        function fn(){
            return function(){}
        }
        fn();
</script>

 

Here return returns a function, then he is a higher-order function

Summary : A higher-order function is a function that operates on other functions. It accepts functions as parameters or outputs functions as return values.

Two, closure

1. What is a closure

A closure isa function that has access to variables in another function 's scope

The simple understanding is that a scope can access local variables inside another function

2. The scope of variables

Let's review the knowledge about the scope of variables first to help us better understand closures

 variable scope

        The scope of variables is divided into two types: global variables and local variables

        1. Global variables can be used inside the function

        2. Local variables cannot be used outside the function

        3. When the function is executed, the local variables in this scope will be destroyed

3. Case

script>
        function fn(){
            num =8
            function fun(){
                console.log(num);
            }
            fun()
        }
        fn()
    </script>

Analysis: First we defined a function named fn, we defined a num = 8 in it, and then we defined a function named fun in the function of fn, and then we printed in the fun function the value of num

4. Result display:

 5. Summary:

We successfully called the variable num in the fn function in the fun function, which is the function that our closure refers to accessing variables in the scope of another function . This is the closure.

3. Recursion

1. What is recursion

 A function is recursive if it can call itself internally

Simple understanding: the function calls itself internally, this function is a recursive function

2. Case 1

function fn(){
    fn()
  }
 fn()

3. Analysis

We use function declaration: declare  function a function with a keyword, and then execute a function name fn. In this way of declaring a function, we need to call the function outside the function, but I not only call it outside the function, but also call it inside the function. The function is to call your own function. This is the simplest case

4. Problem

Using recursion like we did above is prone to " stack overflow" errors (stack overflow). The result is as shown in the figure below. What is the reason? For example, our recursion is actually very similar to our for loop, and it will always call itself , but if there are some variables or data in this function, then he will go to memory to open up space to save these data, then the problem will come, he will always open up space when he keeps calling, then it will lead to " Karma"

5. What is stack overflow?

Every time JavaScript code is executed, a certain size of stack space (1M in Windows system) will be allocated, and certain information (such as parameters, local variables, return values, etc.) will be stored in the stack every time a method is called. Less space will also take up a certain amount of space. If there is more such space, it will exceed the stack space of the thread.
To put it bluntly, it means that regardless of the size of the local data block allocated in the stack, too much data is written to the data block, causing the data to cross the boundary and overwrite other data.

6. Solution

That is, when the result of our code reaches the effect we need, we will end it, that is, add a return 

7. Case 2

<script>
        var num = 1
        function fn(){
            console.log('枕头睡不醒');
            if(num == 10){
                return
            }
            num++
            fn()
        }
        fn()
    </script>

8. Analysis

We first defined a num=1, and we defined a function called fn. The outermost layer must call the fn function, otherwise it will not work, and we call the fn function again in the fn function. We want to print 'can't the pillow wake up', the point is here, we have a judgment condition here, when the number of times of this num = 10, we terminate the function return, and then num++, that is, num after each execution Value +1. In this way, the problem of stack overflow will not occur.

9. Result display:

10. Summary

Recursion means that if a function can call itself internally , then this function is a recursive function. Remember to add return when using recursion! Prevent ' stack overflow ' problems

Guess you like

Origin blog.csdn.net/weixin_52984349/article/details/128068049