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
- Mayor/menor o igual que: <code>a >= b</code>, <code>a <= b</code>.
7
9
- Igual: `a == b` (ten en cuenta el doble signo `=`. Un solo símbolo `a = b` significaría una asignación).
8
10
- Distinto. En matemáticas la notación es <code>≠</code>, pero en JavaScript se escribe como una asignación con un signo de exclamación delante: <code>a != b</code>.
9
11
12
+
En este artículo, aprenderemos más sobre los diferentes tipos de comparaciones, cómo las realiza JavaScript, incluidas las peculiaridades importantes.
13
+
14
+
Al final, encontrará una buena receta para evitar problemas relacionados con "peculiaridades de JavaScript"("javascript quirks").
15
+
10
16
## Booleano es el resultado
11
17
12
18
Como todos los demás operadores, una comparación retorna un valor. En este caso, el valor es un booleano.
@@ -195,7 +201,7 @@ Obtenemos estos resultados porque:
195
201
- Las comparaciones `(1)` y `(2)` retornan `falso` porque `no definido` se convierte en `NaN` y `NaN` es un valor numérico especial que retorna `falso` para todas las comparaciones.
196
202
- La comparación de igualdad `(3)` retorna `falso` porque `undefined` sólo equivale a `null` y a ningún otro valor.
197
203
198
-
### Evita los problemas
204
+
### Evitar los problemas
199
205
200
206
¿Por qué repasamos estos ejemplos? ¿Deberíamos recordar estas peculiaridades todo el tiempo? Bueno, en realidad no. En realidad, estas cosas difíciles se volverán familiares con el tiempo, pero hay una manera sólida de evadir los problemas con ellas:
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/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
@@ -148,7 +148,7 @@ Hay dos formas de hacerlo:
148
148
149
149
1. Multiplicar y dividir.
150
150
151
-
Para redondear el número a dos dígitos tras el decimal, podemos multiplicarlo por `100`, llamar la función de redondeo y volverlo a dividir.
151
+
Para redondear el número a dos dígitos tras el decimal, podemos multiplicarlo por `100` (o una potencia mayor de 10), llamar la función de redondeo y volverlo a dividir.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/03-closure/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
@@ -184,7 +184,7 @@ Los rectángulos en el lado derecho demuestran cómo cambia el entorno léxico g
184
184
185
185
1. Cuando se inicia el script, el entorno léxico se rellena previamente con todas las variables declaradas.
186
186
- Inicialmente, están en el estado "No inicializado". Ese es un estado interno especial, significa que el motor conoce la variable, pero no se puede hacer referencia a ella hasta que se haya declarado con `let`. Es casi lo mismo que si la variable no existiera.
187
-
2. Luego aparece la definición `let phrase`.Todavía no hay una asignación, por lo que su valor es `undefined`. Podemos usar la variable desde este momento.
187
+
2. Luego aparece la definición `let phrase`.Todavía no hay una asignación, por lo que su valor es `undefined`. Podemos usar la variable desde este punto en adelante.
Copy file name to clipboardExpand all lines: 1-js/09-classes/01-class/article.md
+5-26Lines changed: 5 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -331,7 +331,7 @@ alert(user.name); // John
331
331
alert(User.prototype.name); // undefined
332
332
```
333
333
334
-
Technically, they are processed after the constructor has done it's job, and we can use for them complex expressions and function calls:
334
+
We can also assign values using more complex expressions and function calls:
335
335
336
336
```js run
337
337
class User {
@@ -375,30 +375,9 @@ The problem is called "losing `this`".
375
375
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
376
376
377
377
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
378
-
2. Bind the method to object, e.g. in the constructor:
378
+
2. Bind the method to object, e.g. in the constructor.
379
379
380
-
```js run
381
-
classButton {
382
-
constructor(value) {
383
-
this.value= value;
384
-
*!*
385
-
this.click=this.click.bind(this);
386
-
*/!*
387
-
}
388
-
389
-
click() {
390
-
alert(this.value);
391
-
}
392
-
}
393
-
394
-
let button =newButton("hello");
395
-
396
-
*!*
397
-
setTimeout(button.click, 1000); // hello
398
-
*/!*
399
-
```
400
-
401
-
Class fields provide a more elegant syntax for the latter solution:
380
+
Class fields provide another, quite elegant syntax:
402
381
403
382
```js run
404
383
classButton {
@@ -417,9 +396,9 @@ let button = new Button("hello");
417
396
setTimeout(button.click, 1000); // hello
418
397
```
419
398
420
-
The class field `click = () => {...}`creates an independent function on each `Button` object, with `this`bound to the object. Then we can pass `button.click` around anywhere, and it will be called with the right `this`.
399
+
The class field `click = () => {...}`is created on a per-object basis, there's a separate function for each `Button` object, with `this`inside it referencing that object. We can pass `button.click` around anywhere, and the value of `this` will always be correct.
421
400
422
-
That's especially useful in browser environment, when we need to setup a method as an event listener.
401
+
That's especially useful in browser environment, for event listeners.
Copy file name to clipboardExpand all lines: 1-js/09-classes/02-class-inheritance/article.md
+87-2Lines changed: 87 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -230,7 +230,8 @@ let rabbit = new Rabbit("Conejo Blanco", 10); // Error: esto no está definido.
230
230
231
231
Vaya! Tenemos un error. Ahora no podemos crear conejos. ¿Qué salió mal?
232
232
233
-
La respuesta corta es: los constructores en las clases heredadas deben llamar a `super(...)`, y (!) Hacerlo antes de usar `this`.
233
+
La respuesta corta es:
234
+
-**Los constructores en las clases heredadas deben llamar a `super(...)`, y (!) hacerlo antes de usar `this`**.
234
235
235
236
...¿Pero por qué? ¿Que está pasando aqui? De hecho, el requisito parece extraño.
236
237
@@ -243,7 +244,7 @@ Esa etiqueta afecta su comportamiento con `new`.
243
244
- Cuando una función regular se ejecuta con `new`, crea un objeto vacío y lo asigna a `this`.
244
245
- Pero cuando se ejecuta un constructor derivado, no hace esto. Espera que el constructor padre haga este trabajo.
245
246
246
-
Por lo tanto, un constructor derivado debe llamar a `super` para ejecutar su constructor padre (no derivado), de lo contrario no se creará el objeto para `this`. Y obtendremos un error.
247
+
**Por lo tanto, un constructor derivado debe llamar a `super` para ejecutar su constructor padre (no derivado), de lo contrario no se creará el objeto para `this`. Y obtendremos un error.**
247
248
248
249
Para que el constructor `Rabbit` funcione, necesita llamar a `super()` antes de usar `this`, como aquí:
This note assumes you have a certain experience with classes, maybe in other programming languages.
285
+
It provides better insight into the language and also explains the behavior that might be a source of bugs (but not very often).
286
+
If you find it difficult to understand, just go on, continue reading, then return to it some time later.
287
+
```
288
+
289
+
We can override not only methods, but also class fields.
290
+
291
+
Although, there's a tricky behavior when we access an overridden field in parent constructor, quite different from most other programming languages.
292
+
293
+
Consider this example:
294
+
295
+
```js run
296
+
classAnimal {
297
+
name ='animal'
298
+
constructor() {
299
+
alert(this.name); // (*)
300
+
}
301
+
}
302
+
classRabbitextendsAnimal {
303
+
name ='rabbit';
304
+
}
305
+
newAnimal(); // animal
306
+
*!*
307
+
newRabbit(); // animal
308
+
*/!*
309
+
```
310
+
311
+
Here, class `Rabbit` extends `Animal` and overrides `name` field with its own value.
312
+
313
+
There's no own constructor in `Rabbit`, so `Animal` constructor is called.
314
+
315
+
What's interesting is that in both cases: `new Animal()` and `new Rabbit()`, the `alert` in the line `(*)` shows `animal`.
316
+
317
+
**In other words, parent constructor always uses its own field value, not the overridden one.**
318
+
319
+
What's odd about it?
320
+
321
+
If it's not clear yet, please compare with methods.
322
+
323
+
Here's the same code, but instead of `this.name` field we call `this.showName()` method:
324
+
325
+
```js run
326
+
classAnimal {
327
+
showName() { // instead of this.name = 'animal'
328
+
alert('animal');
329
+
}
330
+
constructor() {
331
+
this.showName(); // instead of alert(this.name);
332
+
}
333
+
}
334
+
classRabbitextendsAnimal {
335
+
showName() {
336
+
alert('rabbit');
337
+
}
338
+
}
339
+
newAnimal(); // animal
340
+
*!*
341
+
newRabbit(); // rabbit
342
+
*/!*
343
+
```
344
+
345
+
Please note: now the output is different.
346
+
347
+
And that's what we naturally expect. When the parent constructor is called in the derived class, it uses the overridden method.
348
+
349
+
...But for class fields it's not so. As said, the parent constructor always uses the parent field.
350
+
351
+
Why is there the difference?
352
+
353
+
Well, the reason is in the field initialization order. The class field is initialized:
354
+
- Before constructor for the base class (that doesn't extend anything),
355
+
- Imediately after `super()` for the derived class.
356
+
357
+
In our case, `Rabbit` is the derived class. There's no `constructor()` in it. As said previously, that's the same as if there was an empty constructor with only `super(...args)`.
358
+
359
+
So, `new Rabbit()` calls `super()`, thus executing the parent constructor, and (per the rule for derived classes) only after that its class fields are initialized. At the time of the parent constructor execution, there are no `Rabbit` class fields yet, that's why `Animal` fields are used.
360
+
361
+
This subtle difference between fields and methods is specific to JavaScript
362
+
363
+
Luckily, this behavior only reveals itself if an overridden field is used in the parent constructor. Then it may be difficult to understand what's going on, so we're explaining it here.
364
+
365
+
If it becomes a problem, one can fix it by using methods or getters/setters instead of fields.
In this code, `promises.map` takes input values, turns them into promises (just in case a non-promise was passed) with `p => Promise.resolve(p)`, and then adds `.then` handler to every one.
193
193
194
-
That handler turns a successful result `value` into `{state:'fulfilled', value}`, and an error `reason` into `{state:'rejected', reason}`. That's exactly the format of `Promise.allSettled`.
194
+
That handler turns a successful result `value` into `{status:'fulfilled', value}`, and an error `reason` into `{status:'rejected', reason}`. That's exactly the format of `Promise.allSettled`.
195
195
196
196
Now we can use `Promise.allSettled` to get the results of *all* given promises, even if some of them reject.
197
197
@@ -277,7 +277,7 @@ There are 5 static methods of `Promise` class:
277
277
278
278
1. `Promise.all(promises)` -- waits for all promises to resolve and returns an array of their results. If any of the given promises rejects, it becomes the error of `Promise.all`, and all other results are ignored.
279
279
2. `Promise.allSettled(promises)` (recently added method) -- waits for all promises to settle and returns their results as an array of objects with:
280
-
- `state`: `"fulfilled"` or `"rejected"`
280
+
- `status`: `"fulfilled"` or `"rejected"`
281
281
- `value` (if fulfilled) or `reason` (if rejected).
282
282
3. `Promise.race(promises)` -- waits for the first promise to settle, and its result/error becomes the outcome.
283
283
4. `Promise.resolve(value)` -- makes a resolved promise with the given value.
0 commit comments