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特殊的变量作用域. 变量的作用域 ...
随机推荐
- Docker Got permission denied while trying to connect to the Docker daemon socket at unix://
这是没有权限的原因,先将自己加入docker组,然后在重新启动就可以了, 下面参考来源:https://blog.csdn.net/weixin_40896352/article/details/80 ...
- Mysql的隔离级别
一.首先什么是事务? 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做. 事 ...
- SpringCloud-day09-Feign与Hystrix整合
8.5.Feign 与 Hystrix整合 服务熔断服务降级彻底解耦 前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需 ...
- 《Go程序设计语言》读书笔记-函数
函数包含连续执行的语句,可以使用代码中通过调用函数来执行他们,函数能够将一个复杂的工作切分成多个更小的模块,使多人写作变得容易.另外,函数对他的使用者隐藏了实现细节.这几方面的特性使得函数成为多数编程 ...
- Java框架spring Boot学习笔记(八):@PropertySource,@Value注解
获取配置文件 忽略配置文件不存在时报错
- DVWA中low级的sql注入漏洞的简单复现
第一次成功复现一个简单漏洞,于是写下这篇随笔记录一下 首先我们来看dvwa中low级的sql注入的源码 源码文件路径如下图: 源码如下: <?php if(isset($_GET['Submit ...
- Servlet第三篇(response;request)
response对象 Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象.和代表响应的response对象 那么我们获取浏览器提交过来的数据,找reque ...
- js及vue监听键盘回车事件
js document.onkeydown = (event) => { var e = event || window.event; if(e && e.keyCode==13 ...
- ES6基本使用
var let 度可用于声明变量. 区别:1.let:只在let命令所在代码块内有效 2.let 不存在变量提升(内部影响不到外部) var b = []; ;j<;j++){ let d=j; ...
- oracle自带总页数分页sql
string strSQL = string.Format(@"select * from( with temp as (select * from * where {0} order by ...