当前位置:网站首页>js作用域链与闭包
js作用域链与闭包
2022-07-01 09:13:00 【su27_0101】
谈闭包先谈作用域链
js作用域包含 全局 和 函数 作用域(es5)
如:
var a = 'global var'
function fn(){
var b = 'function var';
var n = function(){
var c = 'inner function var'
}
}
c //; undefined
使用文氏图描述当前作用域划分
闭包可用于突破作用域链
如果要在全局作用域访问内部函数的c变量 需要使用闭包将其与全局作用域相连
var a = 'global var'
function fn(){
var b = 'function var';
var n = function(){
var c = 'inner function var'
return c;
}
return n();
}
c = fn();
c;// 'inner function var'
闭包与GC
js gc 采用 引用计数
我认为 闭包就是利用了 引用计数 特点,使得 局部作用域的变量引用+1 无法释放
边栏推荐
- Simple load balancing with Nacos
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
- Common interview questions for embedded engineers 2-mcu_ STM32
- 2.2 【pytorch】torchvision. transforms
- FAQ | FAQ for building applications for large screen devices
- 类加载
- Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
- Bird recognition app
- 2.2 【pytorch】torchvision.transforms
- 通过 代码实例 理解 浅复制 与 深复制
猜你喜欢

Learning practice: comprehensive application of cycle and branch structure (II)

3D打印Arduino 四轴飞行器

Preparing for the Blue Bridge Cup -- bit operation

【pytorch】nn. Crossentropyloss() and nn NLLLoss()

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

Principle and application of single chip microcomputer timer, serial communication and interrupt system

猿人学第20题(题目会不定时更新)

钓鱼识别app

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()

Redis -- lattice connects to redis cluster
随机推荐
NiO zero copy
序列化、监听、自定义注解
Shell script case in statement
How to realize the usage of connecting multiple databases in idel
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
队列的实现和应用
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
Shell script -select in loop
Flink interview questions
nacos简易实现负载均衡
Flink面试题
Mysql 优化
[interview brush 101] linked list
[pytorch learning] torch device
类加载
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
tensorrt yolov5_ trt. Py comments
树结构---二叉树2非递归遍历
Shell script -for loop and for int loop
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统