Instagram | Twitter | LinkedIn
What is the output of the following code ?
A. undefined
B. Reference Error
C. 🍰
👀 Click here to see the correct answer and explanation
Correct Answer | Explanation |
---|---|
A | The this value (internally) is not actually bound to the arrow function. Normal functions in JavaScript bind their own this value, however the this value used in arrow functions is actually fetched lexically from the scope it sits inside (where the object was defined). In this case, this refers to window object, so, window.cake = undefined . |
Explanation based on 👉🏼 ES6 arrow functions, syntax and lexical scoping & Methods in ES6 objects: using arrow functions
¿ Qué imprime el siguiente código ?
A. undefined
B. Reference Error
C. 🍰
👀 Haz click aquí para ver la respuesta correcta y su explicación
Respuesta correcta | Explicación |
---|---|
A | El valor de this no está realmente vinculado a la función flecha (o arrow function en inglés). Las funciones normales en JavaScript vinculan su propio valor this , sin embargo, el valor de this utilizado en las funciones flecha en realidad se obtiene léxicamente del ámbito en el que se encuentra (en donde se definió el objeto). En este caso, this se refiere al objeto window , por lo que, window.cake = undefined . |
Explicación basada en 👉🏼 ES6 arrow functions, syntax and lexical scoping & Methods in ES6 objects: using arrow functions
Hola @hugok2k ! 😱 Muchas gracias !!! Creo que es porque cambié la posición del emoji y al link olvidé quitarle un guión medio que separaba el emoji del texto 😅 En cuanto tenga un tiempito lo corrijo ! Nuevamente, mil graciasss !