当前位置:网站首页>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问题
边栏推荐
- Tencent jumped out with 38K and saw the real test ceiling
- Rk3399 platform development series explanation (process part) 15.36, understanding process and collaboration process
- 从0开始写bootloader
- JVS基础介绍
- [Numpy] 数组索引和切片
- The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
- Understand the encapsulation and de encapsulation of network model data
- Can tonghuashun open an account on weekends? Is it safe to open an account
- Hexagon_V65_Programmers_Reference_Manual(9)
- Things about stack migration
猜你喜欢

Management of user organization structure

未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)

Download of MySQL driver jar package -- nanny tutorial
![[design tutorial] yolov7 target detection network interpretation](/img/dc/a795dcbd1163df6d8f33704a129d30.png)
[design tutorial] yolov7 target detection network interpretation

Write bootloader from 0

SQL coding bug
![[efficiency] abandon notepad++, this open source substitute is more awesome!](/img/41/495bbe4d1e6d953ba5c4d8984f81e7.jpg)
[efficiency] abandon notepad++, this open source substitute is more awesome!

一周活动速递|深入浅出第8期;Meetup成都站报名进行中

adb shell ls /system/bin(索引表)
![[numpy] array index and slice](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[numpy] array index and slice
随机推荐
Vant component library
金仓数据库 KingbaseES异构数据库移植指南 (2. 概述)
Advanced SQL skills CTE and recursive query
Software test interview question: string "axbyczdj", if you get the result "ABCD"
一文了解Pycharm快捷键
Kingbasees heterogeneous database migration guide (3. Kingbasees migration capability support system)
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
How to monitor the running status and usage of NVIDIA Jetson
2022-07-19 advanced network engineering (XX) BGP route optimization, route optimization analysis one by one
程序中的地址如何转换?
Where is the program?
认识网络模型数据的封装和解封装
【防抖与节流】
[dart] a programming language for cross end development
Things about stack migration
SRE相关问题答疑
After working for bytek for two years, he got 15 offers at one go
How to solve the problem that tp6 controller does not exist: app\controller\index
认识网络模型网络模型概述
Express WEB服务器的简单使用