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/01-getting-started/1-intro/article.md
-9Lines changed: 0 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,21 +106,12 @@ Ferramentas modernas tornam a transpilação muito rápida e transparente, permi
106
106
107
107
Exemplos de tais linguagens:
108
108
109
-
<<<<<<< HEAD
110
109
-[CoffeeScript](http://coffeescript.org/) é um "açúcar sintático" para JavaScript. Ele introduz uma sintaxe mais curta, permitindo-nos escrever um código mais claro e preciso. Normalmente, Ruby devs gostam dele.
111
110
-[TypeScript](http://www.typescriptlang.org/) está concentrado em adicionar "estritos tipos de dados" para simplificar o desenvolvimento e suporte de sistemas complexos. É desenvolvido pela Microsoft.
112
111
-[Flow](http://flow.org/) também adiciona tipos de dados, mas de uma forma diferente. Desenvolvido pela Facebook.
113
112
-[Dart](https://www.dartlang.org/) é uma linguagem autônoma que tem seu próprio interpretador que roda em ambientes fora do navegador (como aplicativos móveis), mas também pode ser transpilada para JavaScript. Desenvolvida pela Google.
114
113
-[Brython](https://brython.info/) é um transpilador de Python para JavaScript que permite escrever aplicativos em puro Python, sem JavaScript.
115
114
-[Kotlin](https://kotlinlang.org/docs/js-overview.html) é uma linguagem de programação moderna, concisa e segura, que pode ser usada no navegador ou no Node.
116
-
=======
117
-
-[CoffeeScript](http://coffeescript.org/) is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
118
-
-[TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
119
-
-[Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
120
-
-[Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
121
-
-[Brython](https://brython.info/) is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
122
-
-[Kotlin](https://kotlinlang.org/docs/reference/js-overview.html) is a modern, concise and safe programming language that can target the browser or Node.
123
-
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
124
115
125
116
Há mais. Claro que, mesmo que usemos uma dessas linguagens transpiladas, também devemos saber JavaScript para entender o que estamos fazendo.
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/01-debugging-chrome/article.md
+1-6Lines changed: 1 addition & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,13 +134,8 @@ Existem botões para isso no topo do painel direito. Vamos interagir com eles.
134
134
135
135
Continuando a clicar nele, passará por todas as instruções do programa, uma por uma.
136
136
137
-
<<<<<<< HEAD
138
137
<spanclass="devtools"style="background-position:-62px-192px"></span> -- "Step over": execute o próximo comando, mas *mas não vá para dentro de uma função*, atalho `key:F10`.
139
-
: Similar ao comando "Step" anterior, mas com um comportamento diferente se a próxima instrução for uma chamada de função. Isto é: não uma incorporada (*built-in*), como `alert`, mas uma função sua.
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
138
+
: Similar ao comando "Step" anterior, mas com um comportamento diferente se a próxima instrução for uma chamada de função. Isto é: não uma incorporada (*built-in*), como `alert`, mas uma função nossa.
144
139
145
140
O comando "Step", vai para dentro dela e suspende a execução na sua primeira linha, ao contrário de "Step over" que executa essa chamada de função aninhada invisívelmente, saltando sobre o funcionamento interno da função.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/10-destructuring-assignment/article.md
+46-45Lines changed: 46 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,6 @@
2
2
3
3
The two most used data structures in JavaScript are `Object` and `Array`.
4
4
5
-
<<<<<<< HEAD
6
-
Objects allow us to pack many pieces of information into a single entity and arrays allow us to store ordered collections. So we can make an object or an array and handle it as a single entity, or maybe pass it to a function call.
7
-
8
-
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes they are more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and soon we'll see how these are handled too.
9
-
=======
10
5
- Objects allow us to create a single entity that stores data items by key.
11
6
- Arrays allow us to gather data items into an ordered list.
12
7
@@ -15,7 +10,6 @@ Although, when we pass those to a function, it may need not an object/array as a
15
10
*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.
16
11
17
12
Destructuring also works great with complex functions that have a lot of parameters, default values, and so on. Soon we'll see that.
18
-
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
19
13
20
14
## Array destructuring
21
15
@@ -27,6 +21,8 @@ let arr = ["John", "Smith"]
27
21
28
22
*!*
29
23
// destructuring assignment
24
+
// sets firstName = arr[0]
25
+
// and surname = arr[1]
30
26
let [firstName, surname] = arr;
31
27
*/!*
32
28
@@ -69,7 +65,7 @@ let [firstName, , title] = ["Julius", "Caesar", "Consul", "of the Roman Republic
69
65
alert( title ); // Consul
70
66
```
71
67
72
-
In the code above, the second element of the array is skipped, the third one is assigned to `title`, and the rest of the array is also skipped.
68
+
In the code above, the second element of the array is skipped, the third one is assigned to `title`, and the rest of the array items is also skipped (as there are no variables for them).
73
69
````
74
70
75
71
````smart header="Works with any iterable on the right-side"
@@ -125,12 +121,8 @@ user.set("name", "John");
125
121
user.set("age", "30");
126
122
127
123
*!*
128
-
<<<<<<< HEAD
129
-
for (let [key, value] of user.entries()) {
130
-
=======
131
124
// Map iterates as [key, value] pairs, very convenient for destructuring
132
125
for (let [key, value] of user) {
133
-
>>>>>>> 468e3552884851fcef331fbdfd58096652964b5f
134
126
*/!*
135
127
alert(`${key}:${value}`); // name:John, then age:30
136
128
}
@@ -267,7 +259,7 @@ Properties `options.title`, `options.width` and `options.height` are assigned to
267
259
The order does not matter. This works too:
268
260
269
261
```js
270
-
// changed the order of properties in let {...}
262
+
// changed the order in let {...}
271
263
let {height, width, title} = { title:"Menu", height:200, width:100 }
272
264
```
273
265
@@ -316,7 +308,7 @@ alert(height); // 200
316
308
317
309
Just like with arrays or function parameters, default values can be any expressions or even function calls. They will be evaluated if the value is not provided.
318
310
319
-
The code below asks for width, but not the title.
311
+
In the code below `prompt`asks for `width`, but not for `title`:
320
312
321
313
```js run
322
314
let options = {
@@ -328,7 +320,7 @@ let {width = prompt("width?"), title = prompt("title?")} = options;
328
320
*/!*
329
321
330
322
alert(title); // Menu
331
-
alert(width); // (whatever you the result of prompt is)
323
+
alert(width); // (whatever the result of prompt is)
332
324
```
333
325
334
326
We also can combine both the colon and equality:
@@ -347,11 +339,26 @@ alert(w); // 100
347
339
alert(h); // 200
348
340
```
349
341
350
-
### The rest operator
342
+
If we have a complex object with many properties, we can extract only what we need:
343
+
344
+
```js run
345
+
let options = {
346
+
title:"Menu",
347
+
width:100,
348
+
height:200
349
+
};
350
+
351
+
// only extract title as a variable
352
+
let { title } = options;
353
+
354
+
alert(title); // Menu
355
+
```
356
+
357
+
### The rest pattern "..."
351
358
352
359
What if the object has more properties than we have variables? Can we take some and then assign the "rest" somewhere?
353
360
354
-
The specification for using the rest operator (three dots) here is almost in the standard, but most browsers do not support it yet.
361
+
We can use the rest pattern, just like we did with arrays. It's not supported by some older browsers (IE, use Babel to polyfill it), but works in modern ones.
355
362
356
363
It looks like this:
357
364
@@ -363,6 +370,8 @@ let options = {
363
370
};
364
371
365
372
*!*
373
+
// title = property named title
374
+
// rest = object with the rest of properties
366
375
let {title, ...rest} = options;
367
376
*/!*
368
377
@@ -371,10 +380,8 @@ alert(rest.height); // 200
371
380
alert(rest.width); // 100
372
381
```
373
382
374
-
375
-
376
-
````smart header="Gotcha without `let`"
377
-
In the examples above variables were declared right before the assignment: `let {…} = {…}`. Of course, we could use existing variables too. But there's a catch.
383
+
````smart header="Gotcha if there's no `let`"
384
+
In the examples above variables were declared right in the assignment: `let {…} = {…}`. Of course, we could use existing variables too, without `let`. But there's a catch.
378
385
379
386
This won't work:
380
387
```js run
@@ -407,14 +414,13 @@ let title, width, height;
407
414
408
415
alert( title ); // Menu
409
416
```
410
-
411
417
````
412
418
413
419
## Nested destructuring
414
420
415
-
If an object or an array contain other objects and arrays, we can use more complex left-side patterns to extract deeper portions.
421
+
If an object or an array contain other nested objects and arrays, we can use more complex left-side patterns to extract deeper portions.
416
422
417
-
In the code below `options` has another object in the property `size` and an array in the property `items`. The pattern at the left side of the assignment has the same structure:
423
+
In the code below `options` has another object in the property `size` and an array in the property `items`. The pattern at the left side of the assignment has the same structure to extract values from them:
418
424
419
425
```js run
420
426
let options = {
@@ -423,10 +429,10 @@ let options = {
423
429
height:200
424
430
},
425
431
items: ["Cake", "Donut"],
426
-
extra:true// something extra that we will not destruct
432
+
extra:true
427
433
};
428
434
429
-
// destructuring assignment on multiple lines for clarity
435
+
// destructuring assignment split in multiple lines for clarity
430
436
let {
431
437
size: { // put size here
432
438
width,
@@ -449,17 +455,11 @@ All properties of `options` object except `extra` that is absent in the left par
449
455
450
456
Finally, we have `width`, `height`, `item1`, `item2` and `title` from the default value.
451
457
452
-
That often happens with destructuring assignments. We have a complex object with many properties and want to extract only what we need.
453
-
454
-
Even here it happens:
455
-
```js
456
-
// take size as a whole into a variable, ignore the rest
457
-
let { size } = options;
458
-
```
458
+
Note that there are no variables for `size` and `items`, as we take their content instead.
459
459
460
460
## Smart function parameters
461
461
462
-
There are times when a function may have many parameters, most of which are optional. That's especially true for user interfaces. Imagine a function that creates a menu. It may have a width, a height, a title, items list and so on.
462
+
There are times when a function has many parameters, most of which are optional. That's especially true for user interfaces. Imagine a function that creates a menu. It may have a width, a height, a title, items list and so on.
463
463
464
464
Here's a bad way to write such function:
465
465
@@ -526,29 +526,28 @@ function showMenu({
526
526
showMenu(options);
527
527
```
528
528
529
-
The syntax is the same as for a destructuring assignment:
529
+
The full syntax is the same as for a destructuring assignment:
530
530
```js
531
531
function({
532
-
incomingProperty:parameterName= defaultValue
532
+
incomingProperty:varName= defaultValue
533
533
...
534
534
})
535
535
```
536
536
537
+
Then, for an object of parameters, there will be a variable `varName` for property `incomingProperty`, with `defaultValue` by default.
538
+
537
539
Please note that such destructuring assumes that `showMenu()` does have an argument. If we want all values by default, then we should specify an empty object:
538
540
539
541
```js
540
-
showMenu({});
541
-
542
+
showMenu({}); // ok, all values are default
542
543
543
544
showMenu(); // this would give an error
544
545
```
545
546
546
-
We can fix this by making `{}` the default value for the whole destructuring thing:
547
-
547
+
We can fix this by making `{}` the default value for the whole object of parameters:
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/10-bind/article.md
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@ libs:
5
5
6
6
# Function binding
7
7
8
-
When using `setTimeout` with object methods or passing object methods along, there's a known problem: "losing `this`".
8
+
When passing object methods as callbacks, for instance to `setTimeout`, there's a known problem: "losing `this`".
9
9
10
-
Suddenly, `this` just stops working right. The situation is typical for novice developers, but happens with experienced ones as well.
10
+
In this chapter we'll see the ways to fix it.
11
11
12
12
## Losing "this"
13
13
14
-
We already know that in JavaScript it's easy to lose`this`. Once a method is passed somewhere separately from the object -- `this` is lost.
14
+
We've already seen examples of losing`this`. Once a method is passed somewhere separately from the object -- `this` is lost.
15
15
16
16
Here's how it may happen with `setTimeout`:
17
17
@@ -37,7 +37,7 @@ let f = user.sayHi;
37
37
setTimeout(f, 1000); // lost user context
38
38
```
39
39
40
-
The method `setTimeout` in-browser is a little special: it sets `this=window` for the function call (for Node.js, `this` becomes the timer object, but doesn't really matter here). So for `this.firstName` it tries to get `window.firstName`, which does not exist. In other similar cases as we'll see, usually `this` just becomes `undefined`.
40
+
The method `setTimeout` in-browser is a little special: it sets `this=window` for the function call (for Node.js, `this` becomes the timer object, but doesn't really matter here). So for `this.firstName` it tries to get `window.firstName`, which does not exist. In other similar cases, usually `this` just becomes `undefined`.
41
41
42
42
The task is quite typical -- we want to pass an object method somewhere else (here -- to the scheduler) where it will be called. How to make sure that it will be called in the right context?
43
43
@@ -102,7 +102,7 @@ The basic syntax is:
102
102
```js
103
103
// more complex syntax will come a little later
104
104
let boundFunc =func.bind(context);
105
-
````
105
+
```
106
106
107
107
The result of `func.bind(context)` is a special function-like "exotic object", that is callable as function and transparently passes the call to `func` setting `this=context`.
108
108
@@ -321,4 +321,8 @@ Also there's a ready [_.partial](https://lodash.com/docs#partial) implementation
321
321
322
322
Method `func.bind(context, ...args)` returns a "bound variant" of function `func` that fixes the context `this` and first arguments if given.
323
323
324
-
Usually we apply `bind` to fix `this` in an object method, so that we can pass it somewhere. For example, to `setTimeout`. There are more reasons to `bind` in the modern development, we'll meet them later.
324
+
Usually we apply `bind` to fix `this` for an object method, so that we can pass it somewhere. For example, to `setTimeout`.
325
+
326
+
When we fix some arguments of an existing function, the resulting (less universal) function is called *partially applied* or *partial*.
327
+
328
+
Partials are convenient when we don't want to repeat the same argument over and over again. Like if we have a `send(from, to)` function, and `from` should always be the same for our task, we can get a partial and go on with it.
0 commit comments