Skip to content

Commit 417b7f8

Browse files
authored
Merge pull request #204 from vplentinax/arrow
Arrow functions revisited
2 parents 29507b0 + b14db8e commit 417b7f8

File tree

1 file changed

+43
-37
lines changed
  • 1-js/06-advanced-functions/12-arrow-functions

1 file changed

+43
-37
lines changed

1-js/06-advanced-functions/12-arrow-functions/article.md

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# Arrow functions revisited
1+
# Funciones de flecha revisadas
22

3-
Let's revisit arrow functions.
3+
Volvamos a revisar las funciones de flecha.
44

5-
Arrow functions are not just a "shorthand" for writing small stuff. They have some very specific and useful features.
65

7-
JavaScript is full of situations where we need to write a small function that's executed somewhere else.
6+
Las funciones de flecha no son solo una "taquigrafía" para escribir pequeñas cosas. Tienen algunas características muy específicas y útiles.
87

9-
For instance:
8+
JavaScript está lleno de situaciones en las que necesitamos escribir una pequeña función que se ejecuta en otro lugar.
109

11-
- `arr.forEach(func)` -- `func` is executed by `forEach` for every array item.
12-
- `setTimeout(func)` -- `func` is executed by the built-in scheduler.
13-
- ...there are more.
1410

15-
It's in the very spirit of JavaScript to create a function and pass it somewhere.
11+
Por ejemplo
1612

17-
And in such functions we usually don't want to leave the current context. That's where arrow functions come in handy.
13+
- `arr.forEach(func)` -- `func` es ejecutado por `forEach` para cada elemento del array.
14+
- `setTimeout(func)` -- `func` es ejecutado por el planificador incorporado.
15+
- ...y muchas más.
1816

19-
## Arrow functions have no "this"
17+
Está en el espíritu de JavaScript crear una función y pasarla a algún otro lugar.
2018

21-
As we remember from the chapter <info:object-methods>, arrow functions do not have `this`. If `this` is accessed, it is taken from the outside.
2219

23-
For instance, we can use it to iterate inside an object method:
20+
Y en tales funciones, por lo general, no queremos abandonar el contexto actual. Ahí es donde las funciones de flecha son útiles.
21+
22+
23+
## Las funciones de flecha no tienen "this"
24+
25+
Como recordamos del capítulo <info:object-methods>, las funciones de flecha no tienen `this`. Si se accede a `this`, se toma el contexto del exterior.
26+
27+
Por ejemplo, podemos usarlo para iterar dentro de un método de objeto:
2428

2529
```js run
2630
let group = {
@@ -39,9 +43,9 @@ let group = {
3943
group.showList();
4044
```
4145

42-
Here in `forEach`, the arrow function is used, so `this.title` in it is exactly the same as in the outer method `showList`. That is: `group.title`.
46+
Aquí en `forEach`, se utiliza la función de flecha, por lo que `this.title` es exactamente igual que en el método externo `showList`. Es decir: `group.title`.
4347

44-
If we used a "regular" function, there would be an error:
48+
Si usáramos una función "regular", habría un error:
4549

4650
```js run
4751
let group = {
@@ -61,28 +65,28 @@ let group = {
6165
group.showList();
6266
```
6367

64-
The error occurs because `forEach` runs functions with `this=undefined` by default, so the attempt to access `undefined.title` is made.
68+
El error se produce porque `forEach` ejecuta funciones con `this = undefined` de forma predeterminada, por lo que se intenta acceder a `undefined.title`.
6569

66-
That doesn't affect arrow functions, because they just don't have `this`.
70+
Eso no afecta las funciones de flecha, porque simplemente no tienen `this`.
6771

68-
```warn header="Arrow functions can't run with `new`"
69-
Not having `this` naturally means another limitation: arrow functions can't be used as constructors. They can't be called with `new`.
72+
```warn header="Las funciones de flecha no pueden ejecutarse con `new`"
73+
No tener `this` naturalmente significa otra limitación: las funciones de flecha no pueden usarse como constructores. No se pueden llamar con `new`.
7074
```
7175
72-
```smart header="Arrow functions VS bind"
73-
There's a subtle difference between an arrow function `=>` and a regular function called with `.bind(this)`:
76+
```smart header="Funciones de flecha VS bind"
77+
Hay una sutil diferencia entre una función de flecha `=>` y una función regular llamada con `.bind(this)`:
7478
75-
- `.bind(this)` creates a "bound version" of the function.
76-
- The arrow `=>` doesn't create any binding. The function simply doesn't have `this`. The lookup of `this` is made exactly the same way as a regular variable search: in the outer lexical environment.
79+
- `.bind(this)` crea una "versión enlazada" de la función.
80+
- La flecha `=>` no crea ningún enlace. La función simplemente no tiene `this`. La búsqueda de 'this' se realiza exactamente de la misma manera que una búsqueda de variable regular: en el entorno léxico externo.
7781
```
7882

79-
## Arrows have no "arguments"
83+
## Las flechas no tienen "argumentos"
8084

81-
Arrow functions also have no `arguments` variable.
85+
Las funciones de flecha tampoco tienen variable `arguments`.
8286

83-
That's great for decorators, when we need to forward a call with the current `this` and `arguments`.
87+
Eso es genial para los decoradores, cuando necesitamos reenviar una llamada con el actual `this` y `argumentos`.
8488

85-
For instance, `defer(f, ms)` gets a function and returns a wrapper around it that delays the call by `ms` milliseconds:
89+
Por ejemplo, `defer (f, ms)` obtiene una función y devuelve un contenedor que retrasa la llamada en `ms` milisegundos:
8690

8791
```js run
8892
function defer(f, ms) {
@@ -96,10 +100,10 @@ function sayHi(who) {
96100
}
97101

98102
let sayHiDeferred = defer(sayHi, 2000);
99-
sayHiDeferred("John"); // Hello, John after 2 seconds
103+
sayHiDeferred("John"); // Hello, John después de 2 segundos
100104
```
101105

102-
The same without an arrow function would look like:
106+
Lo mismo sin una función de flecha se vería así:
103107

104108
```js
105109
function defer(f, ms) {
@@ -112,15 +116,17 @@ function defer(f, ms) {
112116
}
113117
```
114118

115-
Here we had to create additional variables `args` and `ctx` so that the function inside `setTimeout` could take them.
119+
Aquí tuvimos que crear variables adicionales `args` y `ctx` para que la función dentro de `setTimeout` pudiera tomarlas.
120+
121+
## Resumen
122+
123+
Funciones de flecha:
116124

117-
## Summary
118125

119-
Arrow functions:
126+
- No tienen `this`
127+
- No tienen argumentos
128+
- No se pueden llamar con `new`
129+
- Tampoco tienen `super`, que aún no hemos estudiado. Lo veremos en el capítulo <info:class-inheritance>
120130

121-
- Do not have `this`
122-
- Do not have `arguments`
123-
- Can't be called with `new`
124-
- They also don't have `super`, but we didn't study it yet. We will on the chapter <info:class-inheritance>
131+
Esto se debe a que están diseñadas para piezas cortas de código que no tienen su propio "contexto", sino que funcionan en el actual. Y realmente brillan en ese caso de uso.
125132

126-
That's because they are meant for short pieces of code that do not have their own "context", but rather work in the current one. And they really shine in that use case.

0 commit comments

Comments
 (0)