[Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure
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的更多相关文章
- Reading query string values in JavaScript
时间 2016-01-23 13:01:14 CrocoDillon’s Blog 原文 http://crocodillon.com/blog/reading-query-string-valu ...
- [转]UIWebView的Javascript运行时对象
An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...
- W3CSchool实战闯关笔记(JavaScript)
//handsome /** *ugly **/ 第一关注释 // 举例 var myName; // Define myName below this line 第二关声明变量 // Setup v ...
- JavaScript数据类型转换
原文转自:http://javascript.ruanyifeng.com/grammar/conversion.html#rd JavaScript是一种动态类型语言,变量是没有类型的,可以随时赋予 ...
- 什么是JavaScript闭包终极全解之一——基础概念
本文转自:http://www.cnblogs.com/richaaaard/p/4755021.html 什么是JavaScript闭包终极全解之一——基础概念 “闭包是JavaScript的一大谜 ...
- Passing JavaScript Objects to Managed Code
Silverlight If the target managed property or input parameter is strongly typed (that is, not typed ...
- 「译」JavaScript 的怪癖 1:隐式类型转换
原文:JavaScript quirk 1: implicit conversion of values 译文:「译」JavaScript 的怪癖 1:隐式类型转换 译者:justjavac 零:提要 ...
- 【转】javascript日期操作详解(脚本之家整理)
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...
- JavaScript JSON timer(计时器) AJAX HTTP请求 同源策略 跨域请求
JSON 介绍 1. JSON: JavaScript Object Notation 是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是 ...
随机推荐
- noi 2727:仙岛求药
2727:仙岛求药 总时间限制: 1000ms 内存限制: 65536kB 描述 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难 ...
- bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4299 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1 ...
- [BZOJ 4809] 相逢是问候
Link: 传送门 Solution: 以前没见过的套路题…… 1.使用EXT欧拉定理降幂的套路: $a^{x}=a^{xmod\phi(P)+\phi(P)} mod P$,且$x\ge P$ 这样 ...
- lnmp环境一键搭建及卸载
系统需求: CentOS/Debian/Ubuntu Linux系统 需要2GB以上硬盘剩余空间 128M以上内存,OpenVZ的建议192MB以上(小内存请勿使用64位系统) VPS或服务器必须已经 ...
- centos7下redis-2.8.13安装笔记
安装gcc 执行命令:yum install gcc 中间有两次提示,要求输入:yes 或 y,我们按照提示来就可以 至此安装成功 注意:如果不装gcc,解压redis后执行make命令会报错,如下图 ...
- bootstrap字体图标不正常显示的原因
本地引入bootstrap.css文件,使用https://v3.bootcss.com/components/站点 字体图标 时不能正常显示,换成 bootstrap 官网的 cdn 链接却能正常显 ...
- Latest SQLite binary for January 2015
Latest SQLite binary for January 2015 Well I went through quite a few threads to find an updated, de ...
- struts2对拦截器使用带实例
拦截器是struts2的核心.拦截器可以拦截请求,控制视图的走向.那么怎么来实现自定义的拦截器呢? 这里我们做一个例子. 首先假现在做了两个jsp页面一个是登陆的信息的(用session来模拟),一个 ...
- linux 内核升级 网址参考
http://blog.csdn.net/zklth/article/category/826447 http://blog.chinaunix.net/uid-28392723-id-3520177 ...
- VS中运行HTTP 无法注册URL
参考资料 http://www.java123.net/detail/view-449670.html http://www.cnblogs.com/jiewei915/archive/2010/06 ...