作用域,嵌套函数和闭包

  1. <script type="text/javascript">
  2.  
  3. function foo(){
  4. var a = 10;
  5. function bar(){
  6. a *= 2;
  7. }
  8. bar();
  9. return a;
  10. }
  11.  
  12. </script>

在这个示例中,a定义在函数foo中,但函数bar可以访问它,因为bar也定义在foo中。当bar在foo中北调用时它可以访问a,但是如果bar是在foo外部被调用呢?

  1. <script type="text/javascript">
  2.  
  3. function foo(){
  4. var a = 10;
  5. function bar(){
  6. a *= 2;
  7. return a;
  8. }
  9. return bar;
  10. }
  11.  
  12. var baz = foo();
  13. baz(); // return 20
  14. baz(); // return 40
  15. baz(); // return 80
  16.  
  17. var blat = foo();
  18. blat(); // return 20
  19. </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. ios在SQLite3基本操作

    iOS关于sqlite3操作 iPhone中支持通过sqlite3来訪问iPhone本地的数据库. 详细用法例如以下 1:加入开发包libsqlite3.0.dylib 首先是设置项目文件.在项目中加 ...

  2. ImportError with IronPython in C#

    I was using IronPython to execute python code inside my C# implementation lately, and I encountered ...

  3. AWR报告生成

    ORACLE数据库两个比較重要的问题查看报告:awrrpt.sql,ashrpt.sql 生成报告的脚本一般存放在例如以下路径: /home/TEST/db/tech_st/11.2.0/rdbms/ ...

  4. 离PACKET_INp获取信息acket data

         于Floodlight模块假设要packet in消息,就对对应的消息类型进行监听就可以.然后在receive方法中就能够操纵这个上传上来的packet_in.      关键代码:   E ...

  5. JNI 可变印刷

    1.包log.h #ifndef __MULTI_TRACE_H__ #define __MULTI_TRACE_H__ #ifdef ANDROID_NDK_BUILD #define LOG_TA ...

  6. RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools

    Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...

  7. 找呀志_java网络编程(4)TCP/IP、Http和Socket差额

    经java网络编程(1)网络体系结构及通信协议我知道IP协议相应于网络层.TCP协议相应于传输层.而HTTP协议相应于应用层, 三者从本质上来说没有可比性 TPC/IP协议是传输层协议,主要解决数据怎 ...

  8. [精华]Hadoop,HBase分布式集群和solr环境搭建

    1. 机器准备(这里做測试用,目的准备5台CentOS的linux系统) 1.1 准备了2台机器,安装win7系统(64位) 两台windows物理主机: 192.168.131.44 adminis ...

  9. 行政歌节 &#183; 萧谱1

    4之前听 陈越 的<绿野仙踪> 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  10. [three.js] 地图不能解决重复的问题 Solving with Texture RepeatWrapping Fail Issue

    有些事情,如果你正在寻找侯,怎么也找不到. 有的东西,不经意间,到处: 我认为这是生活中常有的事. 然而,在互联网的浩瀚大海,这同样适用. 正常的一小会儿的积累, 投入少, 积累, 洋大海, 载起一帆 ...