function buildCoveTicketMarker(transport){
var passengerNumber = 0;
return function(name){
passengerNumber++;
alert("Ticket via the "
+transport+
"Welcome, "+
name+
"#"+passengerNumber+".");
}
}
var getSubmarineTicket = buildCoveTicketMarker("Submarine");
//passengerNumber number is 1
var getTrianTicket = buildCoveTicketMarker("Train");
//passengerNumber number is 2

The code shows it is still possible the change the variable of the closure in the background.

--------------------------Ex------------------------------------

function warningMaker( obstacle ){
var count = 0;
return function ( number, location ) {
count++;
alert("Beware! There have been " +
obstacle +
" sightings in the Cove today!\n" +
number +
" " +
obstacle +
"(s) spotted at the " +
location +
"!\n"+
"This is Alert #"+
count+
" today for "+
obstacle+
" danger."
);
};
} //Save location also
function warningMaker( obstacle ){
var count = 0;
var locaitons = [];
return function ( number, location ) {
locaitons.push(location);
count++;
alert("Beware! There have been " +
obstacle +
" sightings in the Cove today!\n" +
number +
" " +
obstacle +
"(s) spotted at the " +
location +
"!\n" +
"This is Alert #" +
count +
" today for " +
obstacle +
" danger.\n"+
"Current danger zones are:\n" +
locaitons.map(function(place){return place+"\n";})
);
};
} //Create a zone object to store location and num
function warningMaker( obstacle ){
var count = 0;
var zones = [];
var zone = {};
zone.location = "";
zone.num = 1;
return function ( number, location ) {
count++;
var flag = false;
for(var j = 0; j < zones.length; j++){
if(zones[j].location === location){
zones[j].num++;
flag = true;
}
} if(!flag){
zone.location = location;
zone.num = 1;
zones.push(zone);
} var list = "";
for(var i = 0; i<zones.length; i++){
list = list + "\n" + zones[i].location+" ("+zones[i].num+")";
}
alert("Beware! There have been " +
obstacle +
" sightings in the Cove today!\n" +
number +
" " +
obstacle +
"(s) spotted at the " +
location +
"!\n" +
"This is Alert #" +
count +
" today for " +
obstacle +
" danger.\n" +
"Current danger zones are: " +
list
);
};
}

[Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure的更多相关文章

  1. Reading query string values in JavaScript

    时间 2016-01-23 13:01:14  CrocoDillon’s Blog 原文  http://crocodillon.com/blog/reading-query-string-valu ...

  2. [转]UIWebView的Javascript运行时对象

    An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...

  3. W3CSchool实战闯关笔记(JavaScript)

    //handsome /** *ugly **/ 第一关注释 // 举例 var myName; // Define myName below this line 第二关声明变量 // Setup v ...

  4. JavaScript数据类型转换

    原文转自:http://javascript.ruanyifeng.com/grammar/conversion.html#rd JavaScript是一种动态类型语言,变量是没有类型的,可以随时赋予 ...

  5. 什么是JavaScript闭包终极全解之一——基础概念

    本文转自:http://www.cnblogs.com/richaaaard/p/4755021.html 什么是JavaScript闭包终极全解之一——基础概念 “闭包是JavaScript的一大谜 ...

  6. Passing JavaScript Objects to Managed Code

    Silverlight If the target managed property or input parameter is strongly typed (that is, not typed ...

  7. 「译」JavaScript 的怪癖 1:隐式类型转换

    原文:JavaScript quirk 1: implicit conversion of values 译文:「译」JavaScript 的怪癖 1:隐式类型转换 译者:justjavac 零:提要 ...

  8. 【转】javascript日期操作详解(脚本之家整理)

    时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...

  9. JavaScript JSON timer(计时器) AJAX HTTP请求 同源策略 跨域请求

    JSON 介绍 1. JSON: JavaScript Object Notation 是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是 ...

随机推荐

  1. 订单超时、活动过期解决方案:php监听redis key失效触发回调事件

    Redis 的 2.8.0 版本之后可用,键空间消息(Redis Keyspace Notifications),配合 2.0.0 版本之后的 SUBSCRIBE 就能完成这个定时任务的操作了,定时的 ...

  2. 阿里云无法远程连接数据库MySQL错误码10060解决办法

    使用图形界面管理工具Navicat for MySQL连接Mysql数据库时提示错误:Can't connect to MySQL server (10060) 导致些问题可能有以下几个原因: 1.网 ...

  3. 1265. [NOIP2012] 同余方程

    1265. [NOIP2012] 同余方程 ★☆   输入文件:mod.in   输出文件:mod.out   简单对比 时间限制:1 s   内存限制:128 MB [题目描述] 求关于 x 的同余 ...

  4. python调用matlab

    官网链接: https://ww2.mathworks.cn/help/matlab/matlab_external/call-user-script-and-function-from-python ...

  5. Android 打包出现jdk版本错误的问题

    Android 打包出现 jdk 版本错误的问题,本质上是 SDK 的问题,与 JDK 无关.如果 SDK 的 API 是24或者更高,就要求 jdk 1.8,我这里指定的 API 是22,所以去勾选 ...

  6. 浙江省队选拔 ZJOI2015 (Round 1) 解题报告

    最近莫名其妙地喜欢上了用这种格式写各省省选的全套题解= = 今年浙江省选的出题人是算法竞赛界传说级人物陈立杰,看样子他的出题风格很有特点……ABC三题难度是严格递减的,感觉如果在做第一题的时候被卡住的 ...

  7. MYSQL 源代码学习

    http://ourmysql.com/ http://blog.chinaunix.net/uid-26896862-id-4009777.html

  8. winform打开进程与关闭进程

    #region 判断某进程名是否运行 /// <summary> /// 关闭指定名称的进程 /// </summary> /// <param name="p ...

  9. panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

    go的结构体中私有的属性, 即使反射也获取不到

  10. java二分查找法

    //二分查找法.必须有前提:数组中的元素要有序. public static int halfSeach_2(int[] arr,int key){ int min,max,mid; min = ; ...