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特殊的变量作用域. 变量的作用域 ...
随机推荐
- vue2.0的初始化
vue2.0的初始化,使用 webpack构建工具生成的项目 直接上代码 main.js文件 // The Vue build version to load with the `import` co ...
- 苹果手机input有圆角阴影的解决方法
input[type=button], input[type=submit], input[type=file], button { cursor: pointer; -webkit-appearan ...
- “XmlDocumentationProvider”不实现接口成员“IDocumentationProvider.GetDocumentation(HttpControllerDescriptor)”错误的解决方案
这东西就是这样,会的不难,难的不会.以前我配置过 WebAPI 的 HelpPage 功能,第一步先安装:Microsoft.AspNet.WebAPi.HelpPage,第二步安装:WebApiTe ...
- 详解键盘事件(keydown,keypress,keyup)
一.键盘事件基础 1.定义 keydown:按下键盘键 keypress:紧接着keydown事件触发(只有按下字符键时触发) keyup:释放键盘键 顺序为:keydown -> keypre ...
- Python MD5算法使用
## md5算法简介 1. **简介** MD5消息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值 ...
- Scrapy Spider MiddleWare 设置
# -*- coding: utf-8 -*- # Define here the models for your spider middleware # # See documentation in ...
- ServletConfig、ServletContext 的应用
一.ServletConfig对象及其应用(用的不多) 1. Context和ContextPath:一个web工程,若名为JavaWeb,访问的路径为:http://localhost:8080/J ...
- 使用PHP实现手机端APP支付宝的支付功能
最近应业务需求,做了支付宝支付和微信支付,今天分享一下手机端app支付宝支付对接流程,实际开发过程是前后端分离,前端调用后端API接口,实现功能返回数据,我所用的跨挤啊为TP5,大致可以分为四步: 1 ...
- Codeforces 1086D Rock-Paper-Scissors Champion
Description \(N\) 个人排成一排, 每个人都事先决定出剪刀.石头.布. 每次可以任意选两个相邻的人进行决斗. 规则和游戏一样. 但是如果平局, 则掷硬币来决定胜负. 输的人下场. 现要 ...
- win10jdk环境变量配置问题:'javac' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
在编译时报错:'javac' 不是内部或外部命令,也不是可运行的程序 或批处理文件. 原因1:配置Path的时候使用%JAVA_HOME%相对路径配置. 解决:把Path路径改为绝对路径(例:D:\P ...