Funciones flecha y this
Una forma más corta de escribir funciones, y la palabra que más dolores de cabeza da al empezar.
Arrow functions and this
A shorter way to write functions, and the word that causes the most headaches when starting out.
Ya sabes escribir funciones con function. Una función flecha
hace lo mismo con menos texto, usando =>.
You already know how to write functions with function. An
arrow function does the same with less text, using =>.
Su uso más común no es guardarlas en una variable, sino pasarlas directo como argumento a otra función: para un evento, un temporizador, o un método de array.
Their most common use is not storing them in a variable, but passing them straight in as an argument to another function: for an event, a timer, or an array method.
this es una palabra especial que apunta "al objeto dueño" de la función que se
está ejecutando. Dentro de un método de un objeto, this es ese objeto.
this is a special word that points to "the owning object" of the function
currently running. Inside an object's method, this is that object.
Dentro de anotar, this es
jugador, porque así se llamó al método: jugador.anotar(...).
Inside anotar, this is
jugador, because that is how the method was called:
jugador.anotar(...).
Acá está la trampa: una función flecha no tiene su propio this. Usa el
this del lugar donde fue escrita. Eso la salva de un error muy común.
Here is the catch: an arrow function has no this of its own. It uses the
this from where it was written. That saves it from a very common bug.
Escribir funciones flecha, cuándo son más cortas que function, qué es
this, y por qué una flecha te salva del error más clásico con callbacks.
Ahora, cómo manejar cosas que tardan en llegar —como una respuesta de internet— sin que tu código se enrede.
Writing arrow functions, when they are shorter than function, what
this is, and why an arrow saves you from the most classic callback bug.
Now, how to handle things that take time to arrive — like a response from the internet — without your code turning into a mess.