当前位置:网站首页>js闭包知识
js闭包知识
2022-07-27 18:28:00 【刘鹏远lpy】
一:闭包的三大要素:
1.函数存在嵌套关系
2.内部函数必须引用外部函数的局部变量
3.外部函数必须调用
二:闭包优点:
1.能够在函数外部 访问到内部的变量(原本函数外部无法访问函数内部的局部变量,但函数内部的函数可以访问本函数内的局部变量)
2.延长局部变量的生命周期(本质:)
var fnn2 = fn() 本来执行一次内部变量a就应该销毁,但是因为被赋值了有引用关系,局部变量就一直保存
三: 闭包的缺点
1.滥用闭包,其执行环境一直都在 造成内存溢出 和内存泄漏
四:闭包的生命周期
1.产生:闭包在完成嵌套函数(成立的三大要素)时就产生了(而不是调用)
2.死亡:接收闭包的变量称为垃圾对象时 (null)
五:闭包缺点的解决
1.内存泄漏:内存无法释放
2.内存溢出:内存被撑爆
3.解决:f=null ;让闭包机制清除,f指接收外部函数的那个变量
六:闭包作用域:在函数被创建的地方开始向上寻找
1.函数作为返回值被返回
2.函数作为参数被传递
七:闭包的应用场景
1.做缓存
2.节流,防抖:限制函数的执行次数
3.节流throttle:减少一段时间的触发频率(规定多久以后才能触发一次)
①定时器版本
②时间戳版本:
4.防抖debounce:通过setTimeout的方式,在一的时间间隔内,将多次触发变成一次触发,只执行最后一次
注意this指向和参数arguments问题
边栏推荐
- 金仓数据库 KingbaseES 异构数据库移植指南 (4. 应用迁移流程)
- Can tonghuashun open an account on weekends? Is it safe to open an account
- Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
- 如何解决tp6控制器不存在:app\controller\Index
- Write bootloader from 0
- IOU 目标跟踪其一:IOU Tracker
- 认识传输介质通信方式
- Jetpack compose performance optimization guide - compilation metrics
- 认识网络模型OSI模型
- Babbitt | metauniverse daily must read: Tencent News suspended the sales service of digital collections, users left messages asking for a "refund", and phantom core also fell into the dilemma of "unsa
猜你喜欢

Download of MySQL driver jar package -- nanny tutorial

Flask Mdict builds online MDICT Dictionary Service

go --- air自动重新编译

IOU 目标跟踪其一:IOU Tracker

基于ATX自动化测试解决方案

Introduction to JVs Foundation

After working for bytek for two years, he got 15 offers at one go

全局样式与图标

Go --- automatic recompilation of air

Introduction to rk3399 platform introduction to proficient series (Introduction) 21 day learning challenge
随机推荐
Can tonghuashun open an account on weekends? Is it safe to open an account
How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network
金仓数据库 Oracle 至 KingbaseES 迁移最佳实践 (4. Oracle数据库移植实战)
Tencent jumped out with 38K and saw the real test ceiling
hcip第五天
[numpy] broadcast mechanism
Laboratory management system implemented by SSM framework +jsp [source code + database + system paper]
一周活动速递|深入浅出第8期;Meetup成都站报名进行中
Using dataX to realize efficient synchronization of MySQL data
Write bootloader from 0
Academic sharing | Tsinghua University, Kang Chongqing: power system carbon measurement technology and application (matlab code implementation)
NPDP|什么样的产品经理可以被称为优秀?
User and authority modify user password
认识传输介质通信方式
How to translate the address in the program?
[design tutorial] yolov7 target detection network interpretation
[deep learning] pytoch tensor
R语言使用dplyr包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组均值(mean)并合并生成的统计数据到原数据集中
Why does Alibaba prohibit more than three forms from joining?
Force deduction solution summary 592 fraction addition and subtraction