DEV Community

Cristian Fernando
Cristian Fernando

Posted on

1 1

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

¿Qué imprime este código JavaScript?

let person = { name: "Lydia" };
const members = [person];
person = null;

console.log(members);
Enter fullscreen mode Exit fullscreen mode
  • A: null
  • B: [null]
  • C: [{}]
  • D: [{ name: "Lydia" }]

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:

  • D: [{ name: "Lydia" }]

Cuando hacemos:

const members = [person];
Enter fullscreen mode Exit fullscreen mode

En realidad estamos realizando una copia a la referencia de person, tanto person como members apuntan a la misma referencia del objeto en memoria.

Por este motivo al hacer:

person = null;
Enter fullscreen mode Exit fullscreen mode

Cambiamos el valor de person a null pero members conserva la referencia al objeto y por ello también su valor.

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay