In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "sharing js closures". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "shared js closures".
# # closure
A closure is a function that can read internal variables of other functions:
1. A closure is a function
two。 This function can read variables (local variables) inside other functions.
3. He can keep the read variables in memory all the time.
# # defects in closures:
The variables read by the closure function will be kept in memory all the time, and using it blindly without processing can easily lead to memory leaks (memory waste caused by unreleased or unable to release memory, causing the program to slow down).
# # case
I said a lot of things, and I want some practical information.
This is a case I saw on the Internet.
`
Function fun (NJO) {
Console.log (o)
Return {
Fun: function (m) {
Return fun (mdirection n)
}
}
}
Var a = fun (0); / /?
A.fun (1); / /?
A.fun (2); / /?
A.fun (3) / /?
Var b = fun (0) .fun (1) .fun (2) .fun (3); / /?
Var c = fun (0) .fun (1) / /?
C.fun (2); / /?
C.fun (3)
`
At that time, I glued the code over, looked at the program and analyzed it, and wrote down the process of each analysis.
`
Function fun (n, o) {
Console.log (o)
Return {/ / an object is returned here, which has a fun function
Fun: function (m) {/ / function returns the return value of calling the fun function
Return fun (m, n)
}
}
}
Var a = fun (0); / /? Only parameters are passed, so n is, o is undefined;a, that is, the object returned by fun
/ / a.fun () is the closure function, and the var a = fun (0) will always reside in memory.
A.fun (1); / /? Re-call fun (n, 0), m = 1 ~ 1 ~ n = 0 ~ # console.log (o), and the result is
A.fun (2); / /? 0
A.fun (3); / /? 0
Var b = fun (0) .fun (1) .fun (2) .fun (3); / /? 0 12
/ / 1. Fun (0) is the object, and n = 0 undefined at this time, output undefined
/ / 2. Then call the fun (m) of the object, m = 1, and the return value is the return value of the external fun (n, o), n = 1, o = 0, and the return value is an object, output
/ / 3. Then call the object's fun (m), m = 2, and the return value is the return value of the call external fun (n, o), n = 2, o = 1, and this return value is an object, output 1
/ / 4. Then call the object's fun (m), m = 3, and the return value is the return value of the call external fun (n, o), n = 3, o = 2, and this return value is an object, output 2
/ / Note here is that the values of n and o are different each time, because they are in different function scopes, and a new fun () is called here each time, opening up a new space.
Var c = fun (0) .fun (1) / /? Undefined
/ / the result here is 1, because they are both called through the object c, that is, because they are both in the same function scope.
C.fun (2); / /? 1
C.fun (3); / /? 1
`
The process of analysis was simply a brainstorm, and it was easy to miss. After finishing writing, I hastened to check his answer (I didn't run the program at that time). It was found that `var b = fun (0) .fun (1) .fun (2) .fun (3) The answer here is different. The answer he gave is' undefined, 0,0`, while the result I got is' undefined, 0,1,2`. I looked back and thought that there was no problem with my analysis. At this time, I suddenly thought that I hadn't tried it again, so I ran it quickly, and it turned out to be on my side.
Thank you for reading, the above is the content of "sharing js closures", after the study of this article, I believe you have a deeper understanding of the problem of sharing js closures, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.