Advertisement
avr39ripe

jsFP01ClousureExample

Mar 8th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <script>
  9.         `use strict`
  10.  
  11.         // Variant A
  12.  
  13.         ////[GlobalLexEnv] -> [null]
  14.         //let count;
  15.         ////[GlobalLexEnv| count] -> [null]
  16.  
  17.         ////[counterLexEnv] -> [GlobalLexEnv| count]-> [null]
  18.         //function counter() {
  19.         //   return count++;
  20.         //}
  21.  
  22.         // Varian B
  23.         //[GlobalLexEnv] -> [null]
  24.         //[makeCounterLexEnv| count | res] -> [GlobalLexEnv]-> [null]
  25.         //[resLexEnv] -> [makeCounterLexEnv| count | res] -> [GlobalLexEnv]-> [null]
  26.         //function makeCounter() {
  27.         //    let count = 0;
  28.         //    let res = function() { return count++; };
  29.         //    return res;
  30.         //}
  31.  
  32.         function makeCounter() {
  33.             let count = 0;
  34.             return {
  35.                 set: function (val) { count = val; },
  36.                 get: function () { return count; },
  37.                 reset: function () { count = 0; },
  38.                 counter: function () { return count++; }
  39.             };
  40.         }
  41.  
  42.         {
  43.  
  44.             let countA = makeCounter();
  45.             let countB = makeCounter();
  46.  
  47.             countA.set(26);
  48.             for (let i = 0; i < 5; ++i) {
  49.                 console.log(`countA - ${countA.counter()}`);
  50.             }
  51.  
  52.             countB.set(33);
  53.             for (let i = 0; i < 7; ++i) {
  54.                 console.log(`countB - ${countB.counter()}`);
  55.             }
  56.  
  57.             console.log('#########');
  58.             console.log(`countA - ${countA.get()}`);
  59.             console.log(`countB - ${countB.get()}`);
  60.  
  61.             countA.reset();
  62.             //countB.reset();
  63.  
  64.             console.log('#########');
  65.             console.log(`countA - ${countA.get()}`);
  66.             console.log(`countB - ${countB.get()}`);
  67.         }
  68.        
  69.  
  70.         ////func();
  71.         //{
  72.         //    let hello = counter();
  73.         //    for (let i = 0; i < 5; ++i) {
  74.         //        hello();
  75.         //    }
  76.         //}
  77.        
  78.  
  79.         //for (let i = 0; i < 5; ++i) {
  80.         //    console.log(counter());
  81.         //}
  82.  
  83.         //for (let i = 0; i < 5; ++i) {
  84.         //    console.log(counter());
  85.         //}
  86.  
  87.     </script>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement