当前位置:网站首页>The implementation principle and function of closure and memory leakage
The implementation principle and function of closure and memory leakage
2022-04-22 23:49:00 【As cool as mushrooms】
In simple terms, the function in the closure , It can also be understood as a phenomenon , That is, a function needs to access variables inside another objective function , We need to define another function in the objective function ( To extend the scope chain down a section , The purpose is to take advantage of js When looking for free variables , Features that will be found up level by level along the scope chain ), And put this defined function return come out , For external use . In actual development , Closures are mainly used to encapsulate variables , Convergence authority .
What is a closure ?( Put code )
var value = 3
function bar(){
// Variables declared by outer functions
var value=1;
var name = 'homey'
function foo(){
var value = 2
console.log(name)
console.log(value);
console.log(this.value);
}
return foo();
};
var bar2=bar;
// actually bar() Functions are not disposed of by the garbage collection mechanism just because they are executed
// That's what closures do , call bar() function , It's going to carry out what's inside foo function ,foo At this point, the outer variables will be accessed
bar2();

If for this ill-defined , It can be seen that my early blog JavaScript Medium this Principle and 6 A detailed explanation of common usage scenarios _ A blog as cool as mushrooms -CSDN Blog
foo() contain bar() Closure of internal scopes , Make this scope live... All the time , It won't be disposed of by the garbage collection mechanism , That's what closures do , In order to offer foo() Quote at any time .
The advantages of closures :
- It is convenient to call local variables declared in the context
- The logic is tight , You can create another function in a function , It avoids the problem of transmitting parameters
Disadvantages of closures :
Because of using closures , You can make the function not be destroyed after execution , Keep in Memory in , If you use closures a lot, you can leak memory , Memory consumption is high
The application of closure in practice
function isFirstLoad(){
var list=[];
return function(option){
if(list.indexOf(option)>=0){ // Check whether it exists in the existing array , If there is one, it indicates that it already exists
console.log(' Already exists ')
}else{
list.push(option);
console.log(' First incoming '); // No return true, And enter the data this time
}
}
}
var ifl=isFirstLoad();
ifl("zhangsan");
ifl("lisi");
ifl("zhangsan");

You can see , If the outside world wants to visit _list Variable , Only through the functions I define isFirstLoad To visit , I want to visit _list The outside world only provides isFirstLoad This interface . As for how to operate _list, I've defined , All the outside world can do is use my function , Then pass on several different parameters .
Finally, by the way , It's time to define the scope chain , With whom to execute , It doesn't matter when it is executed .
Above excerpts from
Say the application of a closure in practical development - On the moon - Blog Garden
版权声明
本文为[As cool as mushrooms]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222346056529.html
边栏推荐
- [annual summary] carry forward the past and forge ahead into the future: look back on the unreliable 2021 and hope for the reliable 2022
- Django creates table structure and adds, deletes, modifies and queries database
- 在Spartacus产品明细页面用outlet显示自定义数据
- 共轭梯度法(Conjugate Gradients)(4)
- Redis的基本使用
- [experience sharing] share mangopapa's paper learning experience
- Conjugate gradients (3)
- On LD_ Preload hijacking
- MPP架构概念
- visual studio 总是和搜狗输入法冲突
猜你喜欢

ssh: connect to host 172.31.8.132 port 22: Connection refused
![[newcoder] week 20220422](/img/cd/3d24d3b5be26166ab94742da8c46dd.png)
[newcoder] week 20220422

Vscode uses emmylua plug-in to debug unity project tolua code

2022-04-22: give you a matrix board with the size of m x n to represent the deck, where each cell can be a warship 'x' or an empty space ', Returns the number of warships placed on the deck board. war

Redis Cluster集群收缩主从节点的方法

matlab-day01

"100 million" little technical feelings

2022 hard plus deshmann annual summit: release of a variety of pilot flagship to lead the high-end market

【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)

【Objective-C 高级编程】—— GCD
随机推荐
[PCIe 6.0] new features of PCIe 6.0 - detailed explanation of l0p
【毅力挑战】PCIe 每日一问一答(2022.04 进行中)
Invite you to participate in the ume plug-in development competition
2022 hard plus deshmann annual summit: release of a variety of pilot flagship to lead the high-end market
云计算仿真框架CloudSim介绍(截图版)
Traceup | a powerful tool to reduce stress and achieve more success and help you get rid of work anxiety
Detailed explanation of SQL language
Vs-写汇编
Scheme of fingerprint lock PCB circuit board for suitcase
[perseverance challenge] PCIe ask and answer every day (filed on February 2022)
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
Conjugate gradients (4)
【PCIe 6.0】PCIe 6.0 新特性 - DMWr (Deferrable Memory Write) 详解
Share two recent classic OJ questions (arrange subsequences + find the longest continuous number string in the string)
Typora样式调优
FileNotFoundError: [Errno 2] No such file or directory: 'image/1.jpg'问题解决
居家第二十二天的绿豆芽
[newcoder] week 20220422
LeetCode 1446 - 1449
【leetcode】二叉树,104二叉树的最大深度,543二叉树的直径,124二叉树的最大路径和