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 ...
随机推荐
- 行为驱动开发(BDD)
行为驱动开发(BDD) 引言 BDD是对TDD理念的扩展.BDD强调有利害关系的技术团体和非技术团队都要参与到软件开发过程中.可以把它看成一种强调团体间合作的敏捷方法.大多数采用某种敏捷方法的团队最终 ...
- Android_declare-styleable_自己定义控件的属性
1.简单实例 (1).在res/values文件下定义一个attrs.xml文件 <? xml version="1.0" encoding="utf-8" ...
- 我的CSDN之路
经营CSDN博客刚满一年,能够说CSDN给我们带来了非常大的帮助,这一年里CSDN见证了我们的成长.以下两篇博客就是我这一年在CSDN的缩影. 这篇是我一年前写的,那时候刚申请CSDN不久:http: ...
- C++ 建设者继承
微通道公用号CodingRush 分享编程.算法.机器人leanring.数据挖掘.推荐系统.知识大数据计算框架,欢迎扫码关注. 子类继承了哪些东西? 子类在继承父类的时候,父类的public成员变量 ...
- linux 下上传 datapoint数据到yeelink 修改版本
/*client.c*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...
- HDU 4284 状压dp+spfa
题意: 给定n个点 m条无向边 d元. 以下m行表示每条边 u<=>v 以及花费 w 以下top 以下top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过 ...
- Akka FSM 源代码分析
Akka FSM 源代码分析 萧猛 <simonxiao@qq.com> 啰嗦几句 有限状态机本身不是啥新奇东西,在GoF的设计模式一书中就有状态模式, 也给出了实现的建议.各种语言对状态 ...
- Python日志输出格式和时间格式
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H:%M:% ...
- oracle在imp订单具体解释
oracle在imp订单具体解释 Oracle导入实用程序(Import utility)同意从数据库中提取数据,和写入数据到一个操作系统文件项目.imp所用的基本格式:imp[username[/p ...
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
意甲冠军:给定一个有向图有m单向边缘.免费推断是否两点起来(a可以b要么b可以a或最多彼此),该请求 弱联通重量. 算法: 缩点求强连通分量.然后又一次建图.推断新图是否是一条单链,即不能分叉,假设分 ...