zoukankan      html  css  js  c++  java
  • [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!
    " +
              number +
              " " +
              obstacle +
              "(s) spotted at the " +
              location +
              "!
    "+
              "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!
    " +
              number +
              " " +
              obstacle +
              "(s) spotted at the " +
              location +
              "!
    " +
              "This is Alert #" +
              count +
              " today for " +
              obstacle +
              " danger.
    "+
              "Current danger zones are:
    " +
              locaitons.map(function(place){return place+"
    ";})
             );
      };
    }
    
    //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 + "
    " + zones[i].location+" ("+zones[i].num+")";  
        }
        alert("Beware! There have been " +
              obstacle +
              " sightings in the Cove today!
    " +
              number +
              " " +
              obstacle +
              "(s) spotted at the " +
              location +
              "!
    " +
              "This is Alert #" +
              count +
              " today for " +
              obstacle +
              " danger.
    " +
              "Current danger zones are: " +
              list
             );
      };
    }
  • 相关阅读:
    OpenCV进阶之路:神经网络识别车牌字符
    System.ComponentModel.DataAnnotations.Schema.TableAttribute 同时存在于EntityFramework.dll和System.ComponentModel.DataAnnotations.dll中
    windows server 禁用智能卡服务的步骤
    yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
    解决 CentOS7 安装完成后ifconfig命令不能用
    C#下RSA算法的实现(适用于支付宝和易宝支付)
    centos下问题:connect:network is unreachable
    maven 跳过单元测试
    Docker
    Jenkins和Docker
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3890925.html
Copyright © 2011-2022 走看看