当前位置:网站首页>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问题
边栏推荐
- vi工作模式(3种)以及模式切换(转换)
- 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
- 你了解数据同步吗?
- The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
- 金仓数据库 Oracle至KingbaseES迁移最佳实践(2. 概述)
- 【防抖与节流】
- [Numpy] 广播机制(Broadcast)
- To do the test, you have to go to the big factory and disclose the "hidden rules" of bat big factory recruitment internally
- Software test interview question: count the number in a queue, how many positive numbers and how many negative numbers, such as [1, 3, 5, 7, 0, -1, -9, -4, -5, 8]
- How to monitor the running status and usage of NVIDIA Jetson
猜你喜欢

API Gateway介绍

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

Hexagon_V65_Programmers_Reference_Manual(6)

你了解数据同步吗?

Global styles and icons

Hexagon_V65_Programmers_Reference_Manual(7)

People call this software testing engineer. You're just making a living (with HR interview Dictionary)

【深度学习】Pytorch Tensor 张量

Write bootloader from 0

SLIM:自监督点云场景流与运动估计(ICCV 2021)
随机推荐
Brand list cases
Hcip day 5
IPv4/IPv6、DHCP、网关、路由
Programmer growth Chapter 18: project launch
【深度学习】Pytorch torch.autograd 自动差分引擎
人脸识别5.1- insightface人脸检测模型训练实战笔记
命令行 PDF 转换器:::fCoder 2PDF
一文了解Pycharm快捷键
Download of MySQL driver jar package -- nanny tutorial
[Numpy] 广播机制(Broadcast)
[deep learning] pytoch tensor
未定义变量 “Lattice“ 或类 “Lattice.latticeEasy“(Matlab)
IOU 目标跟踪其二:VIOU Tracker
Using dataX to realize efficient synchronization of MySQL data
Express WEB服务器的简单使用
你了解数据同步吗?
How to translate the address in the program?
【Dart】一门为跨端开发而生的编程语言
How to solve the problem that tp6 controller does not exist: app\controller\index
knife4j通过js动态刷新全局参数