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
             );
      };
    }
  • 相关阅读:
    Node Node
    二叉排序树的实现
    Matlab Tricks(十五) —— 圆的正确画法
    Matlab Tricks(十五) —— 圆的正确画法
    Matlab Tricks(十四) —— 句柄(handle)(图形对象属性的读取与修改)
    Matlab Tricks(十四) —— 句柄(handle)(图形对象属性的读取与修改)
    (机器学习/计算机视觉/深度学习)代码
    (机器学习/计算机视觉/深度学习)代码
    论文的发表流程、会议的组织流程
    论文的发表流程、会议的组织流程
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3890925.html
Copyright © 2011-2022 走看看