DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Edited on

3 1

Paracetamol.js💊| #180: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Básico

const persona = {
  nombre: "Cris",
  saludo() {
    return `Hola, ${nombre}`
  }
}

window.nombre = "Ana";

console.log(persona.saludo()) // ?
Enter fullscreen mode Exit fullscreen mode

A. Hola Ana
B. Hola Cris
C. Hola window
D. Hola undefined

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:
A. Hola Ana
En este ejemplo, añadimos la propiedad Ana al objeto global window, entonces como no especificamos el contexto del método usando this, javascript buscará dicha propiedad en window, y oh sorpresa, la encuentra y entonces la muestra por consola.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay