博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript]Clouse Cove, 2 ,Modifying Bound Values After Closure
阅读量:5276 次
发布时间:2019-06-14

本文共 2231 字,大约阅读时间需要 7 分钟。

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 1var 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 alsofunction 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 numfunction 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

 

转载于:https://www.cnblogs.com/Answer1215/p/3890925.html

你可能感兴趣的文章
hdu (欧拉函数+容斥原理) GCD
查看>>
win7下.NET 2.0未在web服务器上注册的问题
查看>>
优秀的前端
查看>>
员工信息增删改查程序 (大神版)
查看>>
【 2013 Multi-University Training Contest 8 】
查看>>
Python学习笔记 set&&dict
查看>>
图像预处理第8步:紧缩重排数字字符
查看>>
GridView 控件的执行顺序
查看>>
源代码一
查看>>
ATM
查看>>
【转】自旋锁及其衍生锁
查看>>
【转】iOS类似Android上toast效果
查看>>
【转】每天一个linux命令(53):route命令
查看>>
defined 函数使用
查看>>
整数中1出现的次数(从1到n整数中1出现的次数)
查看>>
test命令
查看>>
XGBoost的参数说明
查看>>
足球动图gif(一)
查看>>
Turtle库的学习积累
查看>>
程序员取名:用大数据分析一下宝宝取名时最容易重复的名字
查看>>