Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
Object inside an array keep the old obj reference after changing obj value

could someone please explain me this:

let obj = {name: 'puki'}
const arr = [obj]
arr[0] === obj // true (same ref address)
obj = null
console.log(arr) // [{name: 'puki'}]

how come the array is keeping the old obj ref?

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

In other words, you are not deleting the object, but rather the pointer to it. Object variables are actually pointers to the object itself. So that's why you can have many pointers to same object. Each will affect it. But removing a pointer doesn't remove the object unless it's the final pointer to it.

about 4 years ago · Santiago Gelvez Relatório

0

You are confusing an object reference and the object itself.

In your code obj is a reference (an address) to the actual object {name: 'puki'}

The array also stores the reference (the address) to the actual object.

When you overwrite the reference obj with null you're not actually modifying the actual object {name: 'puki'} or the target of the address, you're just overwriting the reference (pointer) with the null pointer / value. There is no dereferencing in Javascript like it exists in C / C++

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda