[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采用完全独立于语言的文本格式,但是 ...
随机推荐
- Hibernate之Hibernate环境搭建
Hibernate之Hibernate环境搭建 一.Hibernate环境搭建的步骤 1.添加Hibernate && SQLServer 的Jar antlr-2.7.7.jar d ...
- [BZOJ4811][YNOI2017]由乃的OJ(树链剖分+线段树)
起床困难综合症那题,只要从高往低贪心,每次暴力跑一边看这一位输入0和1分别得到什么结果即可. 放到序列上且带修改,只要对每位维护一个线段树,每个节点分别记录0和1从左往右和从右往左走完这段区间后变成的 ...
- DP Training(Updating)
感觉前面做了那么多$dp$全是自己想的还是太少啊…… 好像在LZT的博客上看到了不错的资源?赶紧开坑,以一句话题解为主 Codeforces 419B 第一题就开始盗图 由于只有一个交点,手玩一下发现 ...
- 2017-2018-1 JAVA实验站 第六、七周作业
2017-2018-1 JAVA实验站 第六.七周作业 详情请见团队博客
- hdu 4557 暴力
题意: 作为2013年699万应届毕业生中的一员,由于宏观经济的不景气,小明在毕业当天就华丽丽地失业了! 经历了千难万苦的求职过程,小明特别能理解毕业生的就业之难,所以,他现在准备创建一家专门针对IT ...
- setTimeout 第一个参数类型
读别人代码的时候看到这么一段,很不理解,然后就搜了一下百度 setTimeout / setInterval 第一个参数可以有三种类型: 字符串 . methods . 匿名函数 1.字符串 ...
- Codeforces Round #359 (Div. 1) B. Kay and Snowflake dfs
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the pie ...
- Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力
D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题
A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...