You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/18-javascript-specials/article.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -275,9 +275,15 @@ Abbiamo studiato tre modi per creare funzioni in JavaScript:
275
275
```
276
276
277
277
278
+
<<<<<<< HEAD
278
279
- Le funzioni possono avere variabili locali: queste vengono dichiarate all'interno del ciclo. Queste variabili sono visibili solamente all'interno della funzione.
279
280
- I parametri possono avere valori di default: `function sum(a = 1, b = 2) {...}`.
280
281
- Le funzioni ritornano sempre qualcosa. Se non c'è nessuna istruzione `return`, allora il risultato `undefined`.
282
+
=======
283
+
- Functions may have local variables: those declared inside its body or its parameter list. Such variables are only visible inside the function.
284
+
- Parameters can have default values: `functionsum(a=1, b=2) {...}`.
285
+
- Functions always return something. If there's no `return` statement, then the result is `undefined`.
286
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
281
287
282
288
Per più informazioni: vedi <info:function-basics>, <info:arrow-functions-basics>.
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/01-debugging-chrome/article.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,8 +134,13 @@ Ci sono dei bottoni appositi nella parte superiore del pannello di destra. Provi
134
134
135
135
Continuando a cliccare eseguiremo lo script un passo per volta.
136
136
137
+
<<<<<<< HEAD
137
138
<spanclass="devtools"style="background-position:-62px-192px"></span> -- "Step over": esegue il prossimo comando, ma *non entra nella funzione*, hotkey `key:F10`.
138
139
: Molto simile al comando "Step", ma si comporta diversamente nel caso in cui la prossima istruzione sia una chiamata a funzione. Cioè: non una funzione built-in come `alert`, ma una funzione definita da noi.
140
+
=======
141
+
<spanclass="devtools"style="background-position:-62px-192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
142
+
: Similar to the previous "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
143
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
139
144
140
145
Il comando "Step" entra nella funzione e mette in pausa l'esecuzione, mentre "Step over" esegue la chiamata a funzione, saltandone il contenuto.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/10-destructuring-assignment/article.md
+119-9Lines changed: 119 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
Le due strutture dati più utilizzate in JavaScritp sono `Object` e `Array`.
4
4
5
+
<<<<<<< HEAD
5
6
Gli oggetti ci consentono di raccogliere molti pezzi di informazione in una singola entità, mentre gli array ci consentono di memorizzare collezioni ordinate. Possiamo quindi costruire un oggetto o un array e gestirlo come singola entità, oppure passarlo ad una funzione.
6
7
7
8
*L'assegnamento di destrutturazione* è una speciale sintassi che ci consente di "spacchettare" oggetti o array in un insieme di variabili, che in molti casi possono risultare più comode.
@@ -14,6 +15,24 @@ Un esempio di come un array viene destrutturato in variabili:
14
15
```js
15
16
// abbiamo un array con nome e cognome
16
17
let arr = ["Ilya", "Kantor"]
18
+
=======
19
+
- Objects allow us to create a single entity that stores data items by key.
20
+
- Arrays allow us to gather data items into an ordered list.
21
+
22
+
Although, when we pass those to a function, it may need not an object/array as a whole. It may need individual pieces.
23
+
24
+
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes that's more convenient.
25
+
26
+
Destructuring also works great with complex functions that have a lot of parameters, default values, and so on. Soon we'll see that.
27
+
28
+
## Array destructuring
29
+
30
+
Here's an example of how an array is destructured into variables:
31
+
32
+
```js
33
+
// we have an array with the name and surname
34
+
let arr = ["John", "Smith"]
35
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
17
36
18
37
*!*
19
38
// assegnamento di destrutturazione
@@ -22,20 +41,29 @@ let arr = ["Ilya", "Kantor"]
22
41
let [firstName, surname] = arr;
23
42
*/!*
24
43
25
-
alert(firstName); //Ilya
26
-
alert(surname); //Kantor
44
+
alert(firstName); //John
45
+
alert(surname); //Smith
27
46
```
28
47
29
48
Ora possiamo lavorare con le variabili piuttosto che con i membri dell'array.
30
49
31
50
Risulta utile se combinata con `split` o altri metodi che ritorna un array:
32
51
33
-
```js
34
-
let [firstName, surname] ="Ilya Kantor".split('');
52
+
```js run
53
+
let [firstName, surname] = "John Smith".split(' ');
54
+
alert(firstName); // John
55
+
alert(surname); // Smith
35
56
```
36
57
58
+
<<<<<<< HEAD
37
59
````smart header="\"Destrutturazione\" non significa \"distruzione\"."
38
60
Viene chiamato "assegnamento di destrutturazione", perché "destrutturizza" copiando gli elementi all'interno di variabili. Ma l'array stesso non viene modificato.
61
+
=======
62
+
As you can see, the syntax is simple. There are several peculiar details though. Let's see more examples, to better understand it.
63
+
64
+
````smart header="\"Destructuring\" does not mean \"destructive\"."
65
+
It's called "destructuring assignment," because it "destructurizes" by copying items into variables. But the array itself is not modified.
66
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
39
67
40
68
E' solo un modo breve per scrivere:
41
69
```js
@@ -68,27 +96,38 @@ Nel codice sopra, il secondo elemento viene ignorato, il terzo viene assegnato a
68
96
let [a, b, c] = "abc"; // ["a", "b", "c"]
69
97
let [one, two, three] = new Set([1, 2, 3]);
70
98
```
71
-
99
+
That works, because internally a destructuring assignment works by iterating over the right value. It's kind of syntax sugar for calling `for..of` over the value to the right of `=` and assigning the values.
72
100
````
73
101
74
102
103
+
<<<<<<< HEAD
75
104
````smart header="Assegna a qualsiasi cosa ci sia dalla parte sinistra"
76
105
77
106
Possiamo inserire qualsiasi cosa sia "assegnabile" alla sinistra.
107
+
=======
108
+
````smart header="Assign to anything at the left-side"
Nel capitolo precedente abbiamo visto il metodo [Object.entries(obj)](mdn:js/Object/entries).
127
+
=======
128
+
````smart header="Looping with .entries()"
129
+
In the previous chapter we saw the [Object.entries(obj)](mdn:js/Object/entries) method.
130
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
92
131
93
132
Possiamo utilizzarlo con la destrutturazione per eseguire cicli su chaivi/valore di un oggetto:
94
133
@@ -106,62 +145,115 @@ for (let [key, value] of Object.entries(user)) {
106
145
}
107
146
```
108
147
148
+
<<<<<<< HEAD
109
149
...Lo stesso vale per map:
150
+
=======
151
+
The similar code for a `Map` is simpler, as it's iterable:
152
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
110
153
111
154
```js run
112
155
let user = new Map();
113
156
user.set("name", "John");
114
157
user.set("age", "30");
115
158
116
159
*!*
160
+
// Map iterates as [key, value] pairs, very convenient for destructuring
117
161
for (let [key, value] of user) {
118
162
*/!*
119
163
alert(`${key}:${value}`); // name:John, then age:30
120
164
}
121
165
```
122
166
````
123
167
168
+
<<<<<<< HEAD
124
169
```smart header="Il trucco dello scambio di varibili"
125
170
Un metodo molto conosciuto per lo scambio dei valori di due variabili:
171
+
=======
172
+
````smart header="Swap variables trick"
173
+
There's a well-known trick for swapping values of two variables using a destructuring assignment:
174
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
126
175
127
176
```js run
128
177
let guest = "Jane";
129
178
let admin = "Pete";
130
179
180
+
<<<<<<< HEAD
131
181
// Scambio dei valori: rende guest=Pete, admin=Jane
182
+
=======
183
+
// Let's swap the values: make guest=Pete, admin=Jane
184
+
*!*
185
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
132
186
[guest, admin] = [admin, guest];
187
+
*/!*
133
188
134
189
alert(`${guest} ${admin}`); // Pete Jane (scambiati con successo!)
135
190
```
136
191
137
192
Qui creiamo un array temporaneo di due varibili e lo destrutturiamo immediatamente invertendo l'ordine delle variabili.
138
193
194
+
<<<<<<< HEAD
139
195
In questo modo possiamo invertire l'ordine di più di due variabili.
140
196
197
+
=======
198
+
We can swap more than two variables this way.
199
+
````
200
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
141
201
142
202
### L'operatore resto '...'
143
203
204
+
<<<<<<< HEAD
144
205
Se invece vogliamo destrutturare tutto, non solamente il primo elemento, ma raccogliere anche quello che segue -- passiamo un ulteriore parametro nella forma `"..."`:
206
+
=======
207
+
Usually, if the array is longer when the list at the left, the "extra" items are omitted.
208
+
209
+
For example, here only two items are taken, and the rest is just ignored:
210
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
145
211
146
212
```js run
147
-
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
213
+
let [name1, name2] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
148
214
149
215
alert(name1); // Julius
150
216
alert(name2); // Caesar
217
+
// Furher items aren't assigned anywhere
218
+
```
219
+
220
+
If we'd like also to gather all that follows -- we can add one more parameter that gets "the rest" using three dots `"..."`:
221
+
222
+
```js run
223
+
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
151
224
152
225
*!*
226
+
<<<<<<< HEAD
153
227
// Da notare che il tipo di `rest` è Array.
228
+
=======
229
+
// rest is array of items, starting from the 3rd one
230
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
154
231
alert(rest[0]); // Consul
155
232
alert(rest[1]); // of the Roman Republic
156
233
alert(rest.length); // 2
157
234
*/!*
158
235
```
159
236
237
+
<<<<<<< HEAD
160
238
La variabile `rest` è un array dei valori dell'array rimanenti. Possiamo utilizzare qualsiasi altro nome di variabile al posto di `rest`, è sufficiente accertarsi di inserire i tre punti prima del nome.
239
+
=======
240
+
The value of `rest` is the array of the remaining array elements.
241
+
242
+
We can use any other variable name in place of `rest`, just make sure it has three dots before it and goes last in the destructuring assignment.
243
+
244
+
```js run
245
+
let [name1, name2, *!*...titles*/!*] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
246
+
// now titles = ["Consul", "of the Roman Republic"]
247
+
```
248
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
161
249
162
250
### Valori di default
163
251
252
+
<<<<<<< HEAD
164
253
Se ci sono meno valori nell'array delle variabili da assegnare, non ci sarà alcun errore. I valori assenti vengono considerati undefined:
254
+
=======
255
+
If the array is shorter than the list of variables at the left, there'll be no errors. Absent values are considered undefined:
256
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
165
257
166
258
```js run
167
259
*!*
@@ -186,7 +278,11 @@ alert(surname); // Anonymous (valore di default)
186
278
187
279
I valori di default possono essere anche espressioni complesse o anche delle chiamate a funzione. Verranno presi in considerazione solamente se non verrà fornito alcun valore.
188
280
281
+
<<<<<<< HEAD
189
282
Ad esempio, qui usiamo la funzione `prompt` come default. La chiamata avverrà solamente nel secondo caso:
283
+
=======
284
+
For instance, here we use the `prompt` function for two defaults:
285
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
190
286
191
287
```js run
192
288
// viene eseguito solo il prompt per il cognome
@@ -196,7 +292,7 @@ alert(name); // Julius (dall'array)
196
292
alert(surname); // qualsiasi cosa provenga dal prompt
197
293
```
198
294
199
-
295
+
Please note: the `prompt` will run only for the missing value (`surname`).
200
296
201
297
## Destrutturazione di oggetti
202
298
@@ -208,7 +304,11 @@ La sintassi è:
208
304
let {var1, var2} = {var1:…, var2:…}
209
305
```
210
306
307
+
<<<<<<< HEAD
211
308
Abbiamo un oggetto alla destra dell'assegnazione, che vogliamo dividere in variabili. Nel lato sinistro abbiamo un "pattern" di proprietà corrispondenti. In questo semplice caso, abbiamo una lista di variabili raggruppate tra parentesi `{...}`.
309
+
=======
310
+
We should have an existing object at the right side, that we want to split into variables. The left side contains an object-like "pattern" for corresponding properties. In the simplest case, that's a list of variable names in `{...}`.
311
+
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
212
312
213
313
Ad esempio:
214
314
@@ -228,7 +328,13 @@ alert(width); // 100
228
328
alert(height); // 200
229
329
```
230
330
331
+
<<<<<<< HEAD
231
332
Le proprietà `options.title`, `options.width` e `options.height` vengono assegnate alle variabili corrispondenti. L'ordine non ha importanza. Questo codice funzionerebbe allo stesso modo:
333
+
=======
334
+
Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables.
Il pattern alla sinistra potrebbe essere anche più complesso e specificare una mappatura tra proprietà e variabili.
239
345
346
+
<<<<<<< HEAD
240
347
Se volessimo assegnare una proprietà ad una variabile con un altro nome, ad esempio, la proprietà `options.width` vogliamo inserirla in una variabile chiamata `w`, allora possiamo specificarlo con i due punti:
348
+
=======
349
+
If we want to assign a property to a variable with another name, for instance, make `options.width` go into the variable named `w`, then we can set the variable name using a colon:
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/10-bind/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -247,7 +247,7 @@ The call to `mul.bind(null, 2)` creates a new function `double` that passes call
247
247
248
248
That's called [partial function application](https://en.wikipedia.org/wiki/Partial_application) -- we create a new function by fixing some parameters of the existing one.
249
249
250
-
Please note that here we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
250
+
Please note that we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
251
251
252
252
The function `triple` in the code below triples the value:
Copy file name to clipboardExpand all lines: 1-js/99-js-misc/03-currying-partials/article.md
+4-12Lines changed: 4 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -155,7 +155,7 @@ function curried(...args) {
155
155
if (args.length>=func.length) { // (1)
156
156
returnfunc.apply(this, args);
157
157
} else {
158
-
returnfunctionpass(...args2) { // (2)
158
+
returnfunction(...args2) { // (2)
159
159
returncurried.apply(this, args.concat(args2));
160
160
}
161
161
}
@@ -164,18 +164,10 @@ function curried(...args) {
164
164
165
165
When we run it, there are two `if` execution branches:
166
166
167
-
1.Call now: if passed `args` count is the same as the original function has in its definition (`func.length`) or longer, then just pass the call to it.
168
-
2.Get a partial: otherwise, `func`is not called yet. Instead, another wrapper `pass`is returned, that will re-apply `curried` providing previous arguments together with the new ones. Then on a new call, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
167
+
1.If passed `args` count is the same or more than the original function has in its definition (`func.length`) , then just pass the call to it using `func.apply`.
168
+
2.Otherwise, get a partial: we don't call `func`just yet. Instead, another wrapper is returned, that will re-apply `curried` providing previous arguments together with the new ones.
169
169
170
-
For instance, let's see what happens in the case of `sum(a, b, c)`. Three arguments, so `sum.length = 3`.
171
-
172
-
For the call `curried(1)(2)(3)`:
173
-
174
-
1. The first call `curried(1)` remembers `1` in its Lexical Environment, and returns a wrapper `pass`.
175
-
2. The wrapper `pass` is called with `(2)`: it takes previous args (`1`), concatenates them with what it got `(2)` and calls `curried(1, 2)` with them together. As the argument count is still less than 3, `curry` returns `pass`.
176
-
3. The wrapper `pass` is called again with `(3)`, for the next call `pass(3)` takes previous args (`1`, `2`) and adds `3` to them, making the call `curried(1, 2, 3)` -- there are `3` arguments at last, they are given to the original function.
177
-
178
-
If that's still not obvious, just trace the calls sequence in your mind or on paper.
170
+
Then, if we call it, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
179
171
180
172
```smart header="Fixed-length functions only"
181
173
The currying requires the function to have a fixed number of arguments.
0 commit comments