当前位置:网站首页>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 无法释放
边栏推荐
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- Tree structure -- binary tree 2 non recursive traversal
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
- 毕业季,我想对你说
- [ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
- Key points of NFT supervision and overseas policies
- Tree structure --- binary tree 1
- 美团2022年机试
- Pain points and solutions of fixed assets management of group companies
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
猜你喜欢
Bird recognition app
【检测技术课案】简易数显电子秤的设计与制作
dsPIC30F6014a LCD 方块显示
[pytorch] 2.4 convolution function nn conv2d
小鸟识别APP
Mysql 优化
【电赛训练】红外光通信装置 2013年电赛真题
An overview of the design of royalties and service fees of mainstream NFT market platforms
Learning practice: comprehensive application of cycle and branch structure (II)
钓鱼识别app
随机推荐
I use flask to write the website "one"
队列的实现和应用
Meituan machine test in 2022
Simple load balancing with Nacos
2.4 activation function
樹結構---二叉樹2非遞歸遍曆
猿人学第20题(题目会不定时更新)
【pytorch】softmax函数
Promise asynchronous programming
Interrupt sharing variables with other functions and protection of critical resources
pcl_ Viewer command
Serialization, listening, custom annotation
安装Oracle EE
3D打印Arduino 四轴飞行器
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
Shell脚本-read命令:读取从键盘输入的数据
Principle and application of single chip microcomputer timer, serial communication and interrupt system
【pytorch】2.4 卷积函数 nn.conv2d
3D printing Arduino four axis aircraft
【pytorch】nn. Crossentropyloss() and nn NLLLoss()