In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to parse the closure of javascript. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
What is a closure:
A closure is a reference relationship in which an internal function exists.
This reference points to the local variable object of the external function (provided that the internal function uses the local variable of the external function)
The role of closures:
Prolong the life cycle of external function variable objects
Using closures, you can indirectly access private variables inside a function from outside the function.
1. Common closures function outer () {var a = 1 function inner () {console.log (a) / / 1} inner ()} outer () 2. Function createFunc () {var result = new Array () for (var I = 0; I < 10) ) {result [I] = function () {console.log (I)} return result} var result = createFunc () result [0] () / 10result [1] () / / 10result [2] () / / 10result [3] () / / 10result [4] () / / 10result [5] () / / 10result [6] () / / 10result [7] () / 10
First of all, before the code execution, a global object is created, which contains the global properties, and is placed at the top of the global context scope chain, and also at the top of the scope chain of each function. Take this example as an example. As shown in the figure
After initialization, the code starts to execute, and a new object called Active Object is created, with some parameters in it and pressed into the scope chain of the createFunc function.
Because the function result [I].. is still defined in createFunc, the function forms a scope chain before executing the code.
The createFunc function is executed at this point, and when the pointing is finished, the scope chain in createFunc appears as follows. This is shown in the following figure. At this point, result is an array. And Active object has been removed from the top of the createFunc scope chain.
Start executing result [0] (here take result [0] as an example, everything else is the same), and before executing result [0], you should create a new Active object object and put it in the result [0] execution scope stack. As shown in the figure
At this time, the function needs to access I, but I does not exist in active object, so you need to find it along the scope chain and find I in createFunc, and the value of I is 10, so the final printed value is 10. After the execution of the createFunc, the object it creates is not garbage collected, because the I in result [0] still maintains a reference to the object.
The solution to this example is to set up an immediate execution function, and each subscript corresponding to the function executes immediately. When the function is executed immediately, the correct subscript value exists in the context object of each function.
Function createFunc () {var result = new Array () for (var I = 0; I < 10 ) {result [I] = (function (num) {return function () {console.log (num)}}) (I)} return result} var result = createFunc () result [0] () / 0result [1] () / / 1result [2] () / / 2result [3] () / / 3result [4] () / / 4result [5] () / 5result [6] () / / 6result [7] () / / what can 7JavaScript do? It can make web pages interactive, such as responding to user clicks and providing users with a better experience. two。 You can process forms, verify user input, and provide timely feedback to save users time. 3. The page can be created dynamically according to the user's operation. 4 using JavaScript, you can set up some temporary information that cookie stores on the browser.
After reading the above, do you have any further understanding of how to parse the closure of javascript? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.