Learning JavaScript(0)_Concepts
作用域,嵌套函数和闭包
<script type="text/javascript">
function foo(){
var a = 10;
function bar(){
a *= 2;
}
bar();
return a;
}
</script>
在这个示例中,a定义在函数foo中,但函数bar可以访问它,因为bar也定义在foo中。当bar在foo中北调用时它可以访问a,但是如果bar是在foo外部被调用呢?
<script type="text/javascript">
function foo(){
var a = 10;
function bar(){
a *= 2;
return a;
}
return bar;
}
var baz = foo();
baz(); // return 20
baz(); // return 40
baz(); // return 80
var blat = foo();
blat(); // return 20
</script>
在上述的代码中,所返回的对bar函数的引用被赋予变量baz,函数bar现在是在foo外部被调用,但它依然能够访问a。这是因为JavaScript中的作用域是词法性的,函数式运行在定义它们的作用域中(本例是foo内部的作用域),而不是运行在调用它们的作用域中。只要bar被定义在foo中,它就能访问在foo中定义的所有变量,即使foo的执行已经结束。
这就是闭包的一个例子,在foo返回后,它的作用域被保存下来,但只有它返回的那个函数能够访问这个作用域。在前边的例子中,baz和blat各自拥有这个作用域以及a的一个副本,而且只有它们自己能够对其进行修改。返回一个内嵌函数式创建闭包最常用的手段,其他的手段还有...?
Learning JavaScript(0)_Concepts的更多相关文章
- AMD - Learning JavaScript Design Patterns [Book] - O'Reilly
AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...
- Survey Results for Rebecca Murpheys Learning JavaScript Survey
时间 2016-01-27 05:40:46 Raymond Camden's Blog 原文 http://www.raymondcamden.com/2016/01/25/survey-res ...
- Learning JavaScript with MDN & 使用 MDN 学习 JavaScript
Learning JavaScript with MDN & 使用 MDN 学习 JavaScript Learn JavaScript with MDN 和 MDN 一起学习 JavaScr ...
- Learning JavaScript with MDN (call, apply, bind)
Learning JavaScript with MDN (call, apply, bind) call, apply, bind Object.prototype.toString() 检测 js ...
- Learning JavaScript Design Patterns The Observer Pattern
The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...
- Learning JavaScript Design Patterns The Module Pattern
The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...
- Learning JavaScript Design Patterns The Singleton Pattern
The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...
- Learning JavaScript Design Patterns The Constructor Pattern
In classical object-oriented programming languages, a constructor is a special method used to initia ...
- 课程一(Neural Networks and Deep Learning),第一周(Introduction to Deep Learning)—— 0、学习目标
1. Understand the major trends driving the rise of deep learning.2. Be able to explain how deep lear ...
随机推荐
- JDK基本介绍
JDK这是Java Development Kit 缩写,中国被称为Java开发套件.由SUN该公司提供.这是Java应用程序开发提供了编译和执行环境,所有的Java写程序都依赖于它. JDK能够将J ...
- C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法。
原文:C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法. 一般我们在撰写Windows Phone ...
- 使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)
简述: 结合Spring和Hibernate进行开发 使用@Autowired实现依赖注入, 实现一个学生注册的功能,做一个技术原型 从DAO(Repository) -> Service -& ...
- 举例说,在命令模式(Command Pattern)
在前面加上 谈到命令,大部分的人脑海中会想到以下这幅画面 这在现实生活中是一副讽刺漫画,做决定的人不清楚运行决定的人有何特点,瞎指挥.外行领导内行说的就是这样的.只是在软件设计领域,我们显然要为这 ...
- .c和.h档
可一再声明,但不是很多定义 对于一个项目,我们应该要非常好的处理众多的.c和.h文件 1.通过头文件调用库功能:#include <stdio.h> 在非常多场合,源码不便(或 ...
- 《反project核心原则》说明
致亲爱的中国读者: 大家好 !我是<逆向project核心原理> 作者 李承远(ReverseCore). (韩文博客地址:www.reversecore.com) 首先.非常高兴我的 ...
- fiddler打开后 浏览器就上不了网的解决方法
fiddler设置一下即可 Tools fiddler options connections 不要选中 Act as system proxy on startup
- MySQL各种日期类型与整型(转)
日期类型 存储空间 日期格式 日期范围 datetime 8 bytes YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 t ...
- Multicast on Openstack
I test multicast on openstack. I use external Router in this test. Openstack Environment: Havana (ML ...
- Make a dent in the universe
李自成<一个数学家平反>.就像我第一次读同一,我感到鼓舞的野心文."野心是世界的驱动力的最好的结果几乎所有的工作. " 心野心是要留下点什么在自己身后的永恒价值 在这个 ...