Summary of this week (1)

1. What is a closure? Pros and cons of closures.

        When the return value of a function is another function, and the returned function calls a variable inside its parent function, and the returned function is executed externally, a closure is generated. Closures are functions that can read variables inside other functions.

 Advantages: 1. Variables reside in memory for a long time
             2. Avoid pollution of global variables
             3. Private properties and private methods can be defined

Disadvantages: 1. Permanent memory will increase memory usage Improper use will cause memory leaks
           2. You can change the value of internal variables in the parent function

Closure features:

             1. Function nested function

             2. Functions can refer to external parameters and variables

             3. Parameters and variables will not be recycled by the garbage collection mechanism

2. Change the three methods pointed to by this

1.call()

        Syntax: functionname.call(caller, parameter1, ...)

        Function: When a function is borrowed, it will be executed immediately, and this in the function body will point to the borrower or caller

2.apply()

        Syntax: functionname.apply(caller, [parameters, …])

        Function: When a function is borrowed, it will be executed immediately, and this in the function body will point to the borrower or caller

3.bind()

        Syntax: functionname.bind(caller, parameters, ...)

        Function: When a function is borrowed, it will not be executed immediately, but a new function will be returned. You need to manually call the new function to change the point of this

 

Guess you like

Origin blog.csdn.net/m0_64562972/article/details/126690731