How Does Closure Work in Javascript?
Simply, closure is the scope that it can visite and operate the variables outside of the function when the function is created.
In another words, closure can visite all variables and fuctions only if these variables and functions exist in the scope which
the closure can visit.
Example 1:
var outerValue = 'ninja';
function outerFunction() {
if (outerValue == 'ninja') {
alert('I can see the ninja');
}
}
outerFunction();
Actually, we create a closure but we do not realize its advantage. Next example, we will do something complicated.
Example 2:
<script>
var outerValue = 'ninja';
var later;
function outerFunction() {
var innerValue = 'samural';
function innerFunction() {
alert(outerValue);
alert(innerValue);
}
later = innerFunction;
}
outerFunction();
later();
</script>
Now, we can see something awesome to the closure.
When we create innerFunction in outerFunction, we not only create the inner function but also create a closure which includes the annousement of the inner function and all the variables in the scope that the closure can visit.
How Does Closure Work in Javascript?的更多相关文章
- JavaScript闭包(Closure)学习笔记
闭包(closure)是JavaScript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于JavaScript初学者应该是很有用的. 一.变量的作用域 要理解 ...
- [JS]学习Javascript闭包(Closure)
转自:阮一峰 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于Javascript初学者应该是很有用的. 一.变量的 ...
- 学习Javascript闭包(Closure) by 阮一峰
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...
- JavaScript学习总结(十六)——Javascript闭包(Closure)
原文地址: http://www.cnblogs.com/xdp-gacl/p/3703876.html 闭包(closure)是Javascript语言的一个难点,也是它的特色, 很多高级应用都要依 ...
- 主流JavaScript框架(Dojo、Google Closure、jQuery、Prototype、Mootools和YUI)的分析和对比
本文主要选取了目前比较流行的JavaScript框架Dojo.Google Closure.jQuery.Prototype.Mootools和YUI进行对比,主要是根据网上的资料整理而成,希望可以供 ...
- javascript中的闭包(Closure)的学习
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面是我在网上通过学习阮一峰老师的笔记,感觉总结很不错,特记录于此. 一.变量的作用域 要理解 ...
- 学习Javascript闭包(Closure)及几个经典面试题理解
今天遇到一个面试题,结果让我百思不得其解.后来在查阅了各种文档后,理清了来龙去脉.让我们先来看看这道题: function Foo( ){ var i = 0; return function( ){ ...
- [转载]学习Javascript闭包(Closure)
学习Javascript闭包(Closure) 源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...
- 关于Javascript闭包(Closure)
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...
随机推荐
- Failed to introspect annotated methods on class 异常
用@enable时出现错误 Failed to introspect annotated methods on class 很可能是库和springboot版本不一致
- office2016产品密钥
office2016专业增强版产品密钥: VL批量授权版:QCKNG-29MKJ-74G4B-X7DT8-JFHBB(亲测有效) office2016专业增强版密钥(Retail零售版),可电话激活 ...
- JUnit源码分析 - 扩展 - 自定义RunListener
RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...
- java中mysql查询报错java.sql.SQLException: Before start of result set
异常:java.sql.SQLException: Before start of result set 解决方法:使用rs.getString();前一定要加上rs.next(); sm = con ...
- JMETER java.net.SocketException: Connection reset 报错解决方案
相关值解析MaxUserPort:最大动态端口数(Default = 5000, Max = 65534)TcpTimedWaitDelay:TCP等待延迟时间(30)TcpNumConnection ...
- 2018-2019-2 20165315《网络对抗技术》Exp2 后门原理与实践
2018-2019-2 20165315<网络对抗技术>Exp2 后门原理与实践 一.实验任务 使用netcat获取主机操作Shell,cron启动 使用socat获取主机操作Shell, ...
- AFNetworking Delete请求,报参数为空的错误
使用AFNetWorking进行网络请求的时候,AFNetWorking会默认把get head delete这三个方法的请求参数拼到了url的后面,然后造成body为空,一行代码解决: manage ...
- java使用ffmpeg实现上传视频的转码,提取视频的截图等功能
ffmpeg视频采集功能非常强大,不仅可以采集视频采集卡或USB摄像头的图像,还可以进行屏幕录制,同时还支持以RTP方式将视频流传送给支持RTSP的流媒体服务器,支持直播应用. 1.能支持的格式 ff ...
- python websocket 再线聊天室的 Demo
服务端 import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options import ...
- python AES加密 ECB PKCS5
class AesEbc16: # 按块的大小, 一块一块的加密, 明文和密文长度一样 def __init__(self): self.key = b"123qweqqqwerqwer& ...