Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
can anyone explain me why the answer is { name : 'Linda'};
let person = { name : 'Linda'};
let members = person;
person = null

console.log(members)

the output is {name: 'Linda'} but what i've learnt about reference type is that members and person have the same memory address

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

...but what i've learnt about reference type is that members and person have the same memory address...

Sort of. They both contain a reference to the same object, which is elsewhere in memory (not literally inside the variables). (Whether that's literally a memory address or whatever is an implementation detail you don't need to care about, and may differ from JavaScript engine to JavaScript engine.)

When you do person = null, all you're doing is clearing that reference out of the person variable. Doing that has no effect at all on members or on the object that it refers to.

Let's do this step by step:

let person = { name : 'Linda'};

To handle that, the JavaScript engine creates an object in memory. That object will have some kind of way to refer to it called an object reference. For our purposes, let's say it's Ref1234 (that name has no underlying meaning, I literally just picked it at random so we had a name to use):

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−−−−−−>|   (object)    |
                        +−−−−−−−−−−−−−−−+
                        | name: "Linda" |
                        +−−−−−−−−−−−−−−−+

Then the JavaScript engine stores that reference in the variable person:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−+−−−−>|   (object)    |
                  |     +−−−−−−−−−−−−−−−+
                  |     | name: "Linda" |
                  |     +−−−−−−−−−−−−−−−+
person:  Ref1234 −+

Then we do:

let members = person;

Which just copies the reference in person into members as well:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−+−+−−>|   (object)    |
                  | |   +−−−−−−−−−−−−−−−+
                  | |   | name: "Linda" |
                  | |   +−−−−−−−−−−−−−−−+
person:  Ref1234 −+ |
                    |
members: Ref1234 −−−+

Then we do:

person = null;

All that does is put null in person:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−−−+−−>|   (object)    |
                    |   +−−−−−−−−−−−−−−−+
                    |   | name: "Linda" |
                    |   +−−−−−−−−−−−−−−−+
person:  null       |
                    |
members: Ref1234 −−−+

That doesn't change anything else at all. So

console.log(members)

shows the object.

If you then did members = null; or the members variable went out of scope with no closure retaining it, etc., and if it was the only remaining thing containing Ref1234, then the object could be garbage-collected by the JavaScript engine. But that doesn't happen when you do person = null; because something else (members) has a copy of the reference.

about 4 years ago · Juan Pablo Isaza Denunciar

0

When you assign person to members, member will store the address of person at this moment. Then you assign person to null so that person will point to another address but not change the value of existed address. So members still keep the old address and its value

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda