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 ...
随机推荐
- ios在SQLite3基本操作
iOS关于sqlite3操作 iPhone中支持通过sqlite3来訪问iPhone本地的数据库. 详细用法例如以下 1:加入开发包libsqlite3.0.dylib 首先是设置项目文件.在项目中加 ...
- ImportError with IronPython in C#
I was using IronPython to execute python code inside my C# implementation lately, and I encountered ...
- AWR报告生成
ORACLE数据库两个比較重要的问题查看报告:awrrpt.sql,ashrpt.sql 生成报告的脚本一般存放在例如以下路径: /home/TEST/db/tech_st/11.2.0/rdbms/ ...
- 离PACKET_INp获取信息acket data
于Floodlight模块假设要packet in消息,就对对应的消息类型进行监听就可以.然后在receive方法中就能够操纵这个上传上来的packet_in. 关键代码: E ...
- JNI 可变印刷
1.包log.h #ifndef __MULTI_TRACE_H__ #define __MULTI_TRACE_H__ #ifdef ANDROID_NDK_BUILD #define LOG_TA ...
- RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools
Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...
- 找呀志_java网络编程(4)TCP/IP、Http和Socket差额
经java网络编程(1)网络体系结构及通信协议我知道IP协议相应于网络层.TCP协议相应于传输层.而HTTP协议相应于应用层, 三者从本质上来说没有可比性 TPC/IP协议是传输层协议,主要解决数据怎 ...
- [精华]Hadoop,HBase分布式集群和solr环境搭建
1. 机器准备(这里做測试用,目的准备5台CentOS的linux系统) 1.1 准备了2台机器,安装win7系统(64位) 两台windows物理主机: 192.168.131.44 adminis ...
- 行政歌节 · 萧谱1
4之前听 陈越 的<绿野仙踪> 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- [three.js] 地图不能解决重复的问题 Solving with Texture RepeatWrapping Fail Issue
有些事情,如果你正在寻找侯,怎么也找不到. 有的东西,不经意间,到处: 我认为这是生活中常有的事. 然而,在互联网的浩瀚大海,这同样适用. 正常的一小会儿的积累, 投入少, 积累, 洋大海, 载起一帆 ...