In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of JavaScript closure closure". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of JavaScript closure closure"?
1. What is closure
A closure is a function that has access to variables in the scope of another function. -JavaScript advanced programming
Simply understand that a closure is a function, and its characteristic is that one scope can access local variables within another function.
Take a simple example:
For example, we now have a function in which we define a local variable, and if other scopes can access the local variable, a closure is generated. So we define another function inside the function to see if the scope of the function inside can access the local variables in the outer function.
Function f1 () {var num = 10; function f2 () {console.log (num);} f2 ();} F1 ()
The printed result is:
You can find that the value is printed successfully, so the closure is generated.
But some readers may have doubts: the f2 function itself is inside the F1 function, and it can already use the variables of the parent function. So let's access the variable in the external scope of the F1 function and see what the result is.
We change the call to the f2 function to the return value of the F1 function, and then call the F1 function outside the function, as follows:
Function f1 () {var num = 10; function f2 () {console.log (num);} return f2 ()} var f = F1 (); f ()
At this point, it is equivalent to the external scope of F1 accessing the variables of its internal functions. The print result is as follows:
As you can see, you can also use its internal local variables to generate closures.
So it can be concluded that:
Closure: one scope can access local variables inside another function.
Second, the function of closure
We know that the local variable defined inside the function can only be used inside the function, and it will be destroyed when we have finished using it, but after there is a closure, this local variable will be used outside the function and will not be destroyed until its external caller has called it, so the function of the closure is to extend the scope of the variable.
At this point, I believe you have a deeper understanding of "what is the role of JavaScript closure closure?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.