Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Spread Rest</title>
- </head>
- <body>
- <script>
- const arr = [23, -9, 15, 67];
- //SPREAD OPERATOR
- //con collezioni enumerabili (iterable) come stringhe, array, map, set, oggetti
- // [23, -9, 15, 67] => 23, -9, 15, 67
- //copia di array
- let copia = [...arr];
- writeln(copia);
- let arr2 = [[1,2], [3,4]];
- copia = [...arr2];
- copia[1][0] = 33;
- writeln(copia);
- writeln(arr2);
- const arr3 = [{obj1: 'ciao'}, {obj2: 'a tutti'}];
- copia = [...arr3];
- copia[0].obj1 = 'marameo';
- writeln(arr3[0].obj1);
- arr2 = [100, 200, 300, 400];
- const fusione = [...arr, ...arr2];
- writeln(fusione);
- //stringhe
- const s='sono una stringa'
- const arrChar = [...s];
- writeln(arrChar);
- //oggetti
- let cliente = {cognome: 'picchi', nome: 'Giulio'};
- cliente = {...cliente, codice_fiscale: 'xxxxx'};
- console.log(cliente);
- function MediaDi3(n1, n2, n3)
- {
- return (n1 + n2 + n3)/3;
- }
- arr2 = [10, -19, 78, 56, 0, 345];
- writeln(MediaDi3(...arr2.slice(0,3)));
- //rest, a sinistra dell'operatore di assegnamento
- let [ele1, ele2, ...ilResto] = arr2;
- writeln(ele1);
- writeln(ele2);
- writeln(ilResto);
- let {cognome, ...ilRestoObj} = cliente;
- console.log(cognome);
- console.log(ilRestoObj);
- function somma(...numeri)
- {
- let risultato=0;
- numeri.forEach( ele => risultato+=ele );
- return risultato;
- }
- writeln(somma(1,6,8));
- writeln(somma(1,6,8, 56, 23, -51));
- writeln(somma(...[34, 67, 76]));
- function writeln(messaggio, cornicetta=false)
- {
- document.write(messaggio+"<br>");
- if (cornicetta)
- writeln("-".repeat(40));
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement