当前位置:网站首页>什么是闭包?
什么是闭包?
2022-08-01 13:56:00 【mrcsunflower】
闭包:函数嵌套函数,内部函数就是闭包。
<script>
function outerFun() {
let a = 10;
function innerFun() {
console.log(a);
}
return innerFun;
}
let fun = outerFun();
fun();
</script>正常情况下:函数执行完成,内部变量会销毁(释放内存空间)。
闭包:内部函数没有执行完成,外部函数变量不会被销毁。
应用:封装代码,实现模块化
<script>
let module = (function () {
let a = 10;
let b = 20;
function add() {
return a + b;
}
function sub() {
return a - b;
}
return {
add,
sub
}
})() //立即执行函数,声明时直接调用
let result1 = module.add();
let result2 = module.sub();
console.log(result1);
console.log(result2);
</script>边栏推荐
猜你喜欢
随机推荐
gpio模拟串口通信
牛客刷SQL--4
数据挖掘-03
Istio投入生产的障碍以及如何解决这些问题
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
The obstacles to put Istio into production and how we solve them
性能优化——动画优化笔记
什么是元编程
态路小课堂丨浅谈优质光模块需要具备的条件!
拥抱NFV,Istio 1.1 将支持多网络平面
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
免费使用高性能的GPU和TPU—谷歌Colab使用教程
模型运营是做什么的(概念模型数据库)
8. How does the SAP ABAP OData service support the Create operation
【无标题】
The little thing about Request reuse.The research is understood, and I will report it to you.
如何降低Istio服务网格中Envoy的内存开销
魔众文档管理系统 v5.0.0
tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
易优压双驱挖掘机压路机器类网站源码 v1.5.8









