作用域,嵌套函数和闭包

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

  1. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  2. 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 ...

  3. Learning JavaScript with MDN & 使用 MDN 学习 JavaScript

    Learning JavaScript with MDN & 使用 MDN 学习 JavaScript Learn JavaScript with MDN 和 MDN 一起学习 JavaScr ...

  4. Learning JavaScript with MDN (call, apply, bind)

    Learning JavaScript with MDN (call, apply, bind) call, apply, bind Object.prototype.toString() 检测 js ...

  5. Learning JavaScript Design Patterns The Observer Pattern

    The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...

  6. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  7. Learning JavaScript Design Patterns The Singleton Pattern

    The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...

  8. Learning JavaScript Design Patterns The Constructor Pattern

    In classical object-oriented programming languages, a constructor is a special method used to initia ...

  9. 课程一(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 ...

随机推荐

  1. easyui DateTimeBox OK

    一.datetimebox   ok按钮没有点击事件,但是可以通过onSelect事件模拟出“点击了ok按钮一样的效果”,我的代码: 关键: 0,理解DateTimeBox控件,这个控件是由DateB ...

  2. GRUB2配置详解:默认启动项,超时时间,隐藏引导菜单,配置文件详解,图形化配置

    配置文件详解: /etc/default/grub # 设定默认启动项,推荐使用数字 GRUB_DEFAULT=0 # 注释掉下面这行将会显示引导菜单 #GRUB_HIDDEN_TIMEOUT=0 # ...

  3. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  4. java序列化是什么和反序列化和hadoop序列化

    1.什么是序列化和系列化DE- 神马是序列化它,序列化是内存中的对象状态信息,兑换字节序列以便于存储(持久化)和网络传输.(网络传输和硬盘持久化,你没有一定的手段来进行辨别这些字节序列是什么东西,有什 ...

  5. Ognl底层使用

    今天,在得知ognl采用.在这里和大家分享一下.我希望能帮助. package com.hcj.test; import java.util.ArrayList; import java.util.L ...

  6. JAVA字符串格式化-String.format()使用

    传统型格类型 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象. 熟悉C语言的同学应该记得C语言的sprintf()方法.两者有类似之处.format()方法有两种重载 ...

  7. isset,empty,is_null小知识

    <?php /** 在这项研究开始时,有那么多的人不能很好的运用isset,empty,is_null正确null,false等待值回报值做出正确的推理,在这里,我自己总结通过学习小知识,随后的 ...

  8. HDOJ 4424 Conquer a New Region

    并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Me ...

  9. [模拟Android微信]主界面

    首先看很像模仿: 走出来: 实现过程: 依赖类库:actionbarsherlock 用actionbarsherlock来实现顶部的搜索的效果. tab用的是Viewpaper实现的. 详细细节: ...

  10. 【Android进阶】获取Android软件的版本信息

    PackageInfo pinfo = getPackageManager().getPackageInfo("com.andbase", PackageManager.GET_C ...