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?的更多相关文章

  1. JavaScript闭包(Closure)学习笔记

    闭包(closure)是JavaScript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于JavaScript初学者应该是很有用的. 一.变量的作用域 要理解 ...

  2. [JS]学习Javascript闭包(Closure)

    转自:阮一峰 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于Javascript初学者应该是很有用的. 一.变量的 ...

  3. 学习Javascript闭包(Closure) by 阮一峰

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...

  4. JavaScript学习总结(十六)——Javascript闭包(Closure)

    原文地址: http://www.cnblogs.com/xdp-gacl/p/3703876.html 闭包(closure)是Javascript语言的一个难点,也是它的特色, 很多高级应用都要依 ...

  5. 主流JavaScript框架(Dojo、Google Closure、jQuery、Prototype、Mootools和YUI)的分析和对比

    本文主要选取了目前比较流行的JavaScript框架Dojo.Google Closure.jQuery.Prototype.Mootools和YUI进行对比,主要是根据网上的资料整理而成,希望可以供 ...

  6. javascript中的闭包(Closure)的学习

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面是我在网上通过学习阮一峰老师的笔记,感觉总结很不错,特记录于此. 一.变量的作用域 要理解 ...

  7. 学习Javascript闭包(Closure)及几个经典面试题理解

    今天遇到一个面试题,结果让我百思不得其解.后来在查阅了各种文档后,理清了来龙去脉.让我们先来看看这道题: function Foo( ){ var i = 0; return function( ){ ...

  8. [转载]学习Javascript闭包(Closure)

    学习Javascript闭包(Closure)     源地址: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures ...

  9. 关于Javascript闭包(Closure)

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...

随机推荐

  1. H5-手机震动

    //手机震动function vibration(){ navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navi ...

  2. ALV 动态行列

    动态ALV显示的行列,解决部分报表需求. 运行截图如下: 选择屏幕: ALV输出效果1: ALV输出效果2:: ABAP代码: *&------------------------------ ...

  3. spring项目启动时执行任务

    https://www.jianshu.com/p/745868a34379

  4. SpringCloud-day04-Eureka高可用集群配置

    5.4Eureka高可用集群配置 在高并发的情况下一个注册中心难以满足,因此一般需要集群配置多台. 我们再新建两个module  microservice-eureka-server-2002,  m ...

  5. 利用Access-Control-Allow-Origin响应头解决跨域请求原理

    传统的跨域请求没有好的解决方案,无非就是jsonp和iframe,随着跨域请求的应用越来越多,W3C提供了跨域请求的标准方案(Cross-Origin Resource Sharing).IE8.Fi ...

  6. 自己电脑组一个Wifi热点

    ① 在cmd窗口 ssid 是wifi名称.key是密码 netsh wlan set hostednetwork mode=allow ssid=yb key=15564130 ② ③ 运行脚本 新 ...

  7. idea激活方式

    idea破解方法:转自https://note.youdao.com/ynoteshare1/index.html?id=d0f3fb42bfa1bb65a33115a1b6140f5f&ty ...

  8. Android 代码判断是否有网络

    public void okGo() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemServic ...

  9. CP IPS功能测试

    测试环境:CP 15000硬件 测试拓扑: Step1:重新安装Check_Point_R80.10_T479_Gaia并且打补丁 Step2:初始化CP并且部署模式为Management和Gatew ...

  10. java关于get/post请求

    package com.study.test; import java.io.BufferedReader;import java.io.IOException;import java.io.Inpu ...