当前位置:网站首页>闭包的简单理解
闭包的简单理解
2022-06-11 16:54:00 【浩月.】
闭包打通了一条在函数外部访问函数内部作用域的通道, 正常情况下函数外部是访问不到函数内部作用域变量的。
表象判断是不是闭包:
1. 函数嵌套函数
2. 内部函数被return
3. 内部函数调用外层函数的局部变量
应用场景:
1. 函数作为返回值
function fn() {
var count = 1
function fn2() {
console.log(count)
}
//返回一个函数,这个函数就是一个闭包
return fn2
}
//f1 就是 box 函数
var f1 = fn()
//5秒以后调用f1函数,还是能打印count的值, 闭包能让局部变量常驻内存
setTimeout(function () {
f1()
}, 5000) function fn() {
var count = 1
function fn2() {
count += 1
return count
}
return fn2
}
var f1 = fn()
console.log(f1())
console.log(f1())2. 函数作为参数被传递(回调函数)
边栏推荐
- Difference between select into from and insert into select
- 所见即所得的 markdown 编辑器:Typora
- Weekly recommended short video: rookie CEO talks about the new logistics track in the future
- Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
- 笔记本连接外部键盘后,如何把笔记本自身的键盘禁用
- JSP page initial loading method
- error:指针作为函数参数错误总结
- Opencv相机标定之圆形标识点中心检测
- 2022G1工业锅炉司炉考题及模拟考试
- ^32执行上下文栈面试题
猜你喜欢

2022年安全员-B证国家题库及模拟考试

LeetCode——42. 接雨水(双指针)

A set of ThinkPHP wechat applet mall source code with background management

二级造价工程师值得考吗?发展前景如何?

2022年R1快開門式壓力容器操作考試題庫及模擬考試

Elasitcsearch基础学习笔记(1)

Common tools and commands for information collection

【clickhouse专栏】新建库、用户及角色

2022年高处安装、维护、拆除考试模拟100题及在线模拟考试

RSP:遥感预训练的实证研究
随机推荐
Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
Characteristics of interfaces & comparison between interfaces and abstract classes
AXI协议基础知识
基于文本驱动用于创建和编辑图像(附源代码)
jsp页面初始加载方式
“is-a”,“has-a”,“like-a”
Analysis report on the "fourteenth five year plan" proposal and innovation environment of global and Chinese sodium pyrophosphate industry (2022-2028)
2022起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试
Analysis report on competition pattern and future development strategy of China's corn industry 2022-2028 Edition
2022年安全员-B证国家题库及模拟考试
Classic reading of multi task learning: MMOE model
golang中的异常处理和异常使用
关联关系
Char array parsing
Go quick start of go language (I): the first go program
solr(一)solr的安装及权限控制
核密度估计(二维、三维)
A set of ThinkPHP wechat applet mall source code with background management
Student website template brown cake dessert website design - brown cake dessert shop (4 pages) gourmet dessert website production final assignment finished product_ Fresh fruit web design final assign
Web security - shooting range notes