当前位置:网站首页>closures in js
closures in js
2022-08-03 04:32:00 【weixin_46051260】
Concept: A closure is a function that has access to a variable in the scope of another function
1) Features of closures
- Function Nested Function
- A function can reference external parameters and variables
- Parameters and variables will not be garbage collected
2) Advantages of closures
- Variables reside in memory for long periods of time
- Avoid pollution of global variables
3) Disadvantages of closures
Variables are always kept in memory, too many closures may lead to memory leaks
4) Closure scene
- Callbacks
In timers, event listeners, Ajax requests, cross-window communication, Web Workers, or any async, as long as you use a callback function
, you're actually using a closure. - Execute function immediately
- Anti-shake
The callback is executed after n seconds after the event is triggered. If it is triggered again within these n seconds, the timer will be restarted.
边栏推荐
猜你喜欢
随机推荐
TCP 和UDP 的详细介绍
leetcode刷题学习之路
Jmeter 模拟多用户登录的两种方法
redis键值出现 xacxedx00x05tx00&的解决方法
MySQL 出现 The table is full 的解决方法
关于#sql#的问题,如何解决?
SM30 表维护视图数据保存前 数据校验事件
7.Keras开发简介
ScanNet数据集讲解与点云数据下载
那些让电子工程师崩溃瞬间,你经历了几个呢?
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
11.机器学习基础:机器学习的四个分支
MCM箱模型建模方法及大气O3来源解析
Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
【Harmony OS】【FAQ】鸿蒙问题合集1
rosbag工具plotjuggler无法打开rosbag的问题
私域流量引流方法?分享购火爆的商业模式,你值得拥有
【Harmony OS】【ARK UI】ETS 上下文基本操作
计网试卷概念
接口测试实战| GET/POST 请求区别详解