Advertisement
fcamuso

Javascript Lezione 65

Aug 9th, 2022
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <head>
  3.   <meta charset="UTF-8">
  4.   <title>Destructuring</title>
  5. </head>
  6. <body>
  7.   <script>
  8.    
  9.     function min_max(vettore)
  10.     {
  11.       let pos_min=0; let pos_max=0;
  12.  
  13.       //immaginiamo qui l`algoritmo che determina le
  14.       //posizioni dell`elemento minimo e massimo
  15.       pos_min=3; pos_max=7;
  16.      
  17.       return [pos_min, pos_max];
  18.     }
  19.  
  20.  
  21.      //il destructuring funziona anche con gli array
  22.     //(anzi con qualunque collezione iterabile)
  23.     let temperature = [-1,-6,-2,3,8,6,4];
  24.     let [h00, h3, h6, h9, h12, h15, h18,h21] = temperature;
  25.     //writeln(`Temp. alle h06:00 ${h6}โ„ƒ; alle h18:00 ${h18}โ„ƒ`);
  26.  
  27.     ([h00,,,,h12,,,h21=0] = temperature);
  28.     console.log(`${h00} ${h12} ${h21}`);
  29.  
  30.     //swap di variabili
  31.     let x1 = 3, x2 = 9;
  32.    
  33.     // let temp = x1;
  34.     // x1 = x2;
  35.     // x2= temp;
  36.  
  37.     [x2, x1] = [x1, x2];
  38.     console.log(x1, x2);
  39.  
  40. //-------------------------------------------------------------------------
  41.  
  42.     //il destructuring รจ utile anche per gestire return multipli
  43.     let [pos_temp_min, pos_temp_max] = min_max(temperature);
  44.     writeln(`Temperatura minima alle ${3*pos_temp_min}`);
  45.     writeln(`Temperatura massima alle ${3*pos_temp_max}`);
  46. //-------------------------------------------------------------------------
  47.    
  48.  
  49.  
  50.   function writeln(messaggio, separatore=false)
  51.   {
  52.      document.write(messaggio+"<br>");
  53.      if (separatore)
  54.        writeln("-".repeat(40));
  55.   }
  56.  
  57.  </script>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement