[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采用完全独立于语言的文本格式,但是 ...
随机推荐
- [COGS2479]偏序
[COGS2479]偏序 题目大意: \(n(n\le50000)\)个四元组,求四维偏序. 思路: CDQ分治套CDQ分治套树状数组. 细节: 第二层CDQ之前要备份数组\(a\),否则第二层CDQ ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- Spring @Value 用法小结,#与$的区别
20161016更新:这货其实是SpEL的功能,来这里看看吧: Spring 4 官方文档学习(五)核心技术之SpEL 起因 一直的用法是 @Value("${jdbc.driverClas ...
- [Dynamic Language] pyspark Python3.7环境设置 及py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe解决!
pyspark Python3.7环境设置 及py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spa ...
- kgtp linux内核调试
作者:朱辉 开源网址:https://github.com/teawater http://teawater.github.io/kgtp/ 有中文版说明 内核编绎: General set ...
- socket缓冲区以及阻塞模式
socket缓冲区 每个 socket 被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区. write()/send() 并不立即向网络中传输数据,而是先将数据写入缓冲区中,再由TCP协议将数据从 ...
- java基础学习总结——网络编程
一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
- andriod获得应用程序的Context
getApplicationContext() getResources().getString(R.string.app_name) //获得程序名称
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
背景 这两天温习了 5 中排序算法,之前也都看过它们的实现,因为没有深入分析的缘故,一直记不住谁是谁,本文就记录一下我学习的一些心得. 三种排序算法可以总结为如下: 都将数组分为已排序部分和未排序部分 ...