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

167
Visualizações
object freeze does not freeze the object when it is assigned to a new object

I have 2 questions here:-

  1. Why obj2 = 5; is not throwing the error?

  2. If I wish to throw during assignment obj2 = 5; what should I do? Considering obj2 is var or let.

"use strict"

function deepFreeze(object) {
    // Retrieve the property names defined on object
    const propNames = Object.getOwnPropertyNames(object);
  
    // Freeze properties before freezing self
  
    for (const name of propNames) {
      const value = object[name];
  
      if (value && typeof value === "object") {
        deepFreeze(value);
      }
    }
  
    return Object.freeze(object);
  }
  
  let obj2 = {
    internal: {
      a: null
    }
  };
  
  deepFreeze(obj2);
  
//   obj2.internal.a = 'anotherValue'; // fails silently in non-strict mode
//   obj2.internal.a; // null

obj2 = 5; // I want to throw error in this case but it is not throwing error. 
about 4 years ago · Juan Pablo Isaza
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