— Short step, not even thousands of miles, not small streams, beyond into rivers and seas.
(a) can not say the JS key knowledge ~ immediately execute the function
(two) can not say the JS key knowledge ~ throttling and anti shake (test little sister topic)
(3) can not say the JS key knowledge ~ recursion and tail recursion (test little sister topic)
(4) can not say the JS key knowledge ~ closure (test little sister topic)
(5) javaScript key knowledge ~~~ singleton pattern (test little sister topic)
Hello, boys and girls, I will share some grounding knowledge points with you in the future. Such knowledge points can be used in work and study, and then summed up as their own things. This is the ultimate purpose of knowledge sharing.
Together, we get better every day. It all adds up, and one day it will.
What is an immediate function?
Declaring a function and calling the anonymous function immediately is called an immediate function; Alternatively, executing a function immediately is a syntax that allows your function to be executed immediately after it is defined.
(function() {// code})();Copy the code
Second, the function of immediate execution
- You don’t have to name functions to avoid contaminating global variables
- The immediate function creates a separate scope inside that encapsulates private variables that cannot be read externally
- Encapsulation variable
- In summary: executing functions immediately creates a separate scope, and we can encapsulate temporary or local variables to avoid contaminating global variables
Execute the parameters of the function immediately
(function(j){// use j})(I)Copy the code
If a global variable is needed in the immediate function, it is passed as an argument to the immediate function (I in the above example is a global variable, I is the argument, and j is the parameter of I in the immediate function).
Execute the return value of the function immediately
Just like any other function, an immediate function can have a return value. In addition to returning values of primitive types, immediate functions can also return values of any type, such as objects and functions.
let result = ( function(j){ return function(j){console.log(j*j)} } )(i) console.log(result(5)); The result: 25Copy the code
5. Relevant interview questions
for(var i = 0 ; i<3; i++){ setTimeout( function(){console.log(i)} ) }Copy the code
Because I runs through the scope, setTimeout is delayed, and asynchrony must print console after for runs, at which point I is already 3.
Method 1: create a separate scope for each li using the immediate function. When the immediate function is executed, the value of I ranges from 0 to 2 for three immediate functions, so it will output normally.
for(var i = 0 ; i<3; i++){ setTimeout( ( function(){console.log(i)} )() ) }Copy the code
** Method 2: ** Use ES6’s block-level scope.
for(let i = 0 ; i<3; i++){ setTimeout( function(){ console.log(i) } ) }Copy the code
Qinyuanchun · Changsha
Independent cold autumn, xiangjiang North, Orange island head. See wanshan red, all dyed; They are thriving. Eagles strike the sky, fish hang shallow, all kinds of frosty day race free. Who is the master of the ebb and flow of boundless earth? Brought to have swam. Memories of the past are thick. Just the students young, in their prime; Scholarly spirit, violent party. Pointing jiangshan, inspire the text, when the dung wanhu. Ever remember, to the middle of the water, waves to suppress the boat?