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

95
Visualizações
Object.create and delete property from newly created object

If Object.create makes new deep copy of the object, what should happen post deleting the property from the newly created object?

Expected Output: Undefined

Actual Output: Karna

Code

var obj1 = {name:"Karna",loc:"Bengaluru"};

var obj2 = Object.create(obj1);

delete obj2.name;

console.log(obj2.name);

Could you please help me to understand why obj2.name still referring to obj1's property?

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

0

Object.create creates a new object with the argument object as prototype of the new object created (refer to the __proto__ of the newly created object). It does not add the properties of obj1 to obj2. So, if you invoke delete on obj2, it will delete the properties of obj2, and not the ones in the prototype.

Example:

var obj1 = { name: 'John' }

var obj2 = Object.create(obj1);

console.log(obj2.__proto__ === obj1) // returns true

console.log(obj2.hasOwnProperty('name')) // returns false; 

console.log(obj2.name) // returns John 

delete obj2.name; // returns true

console.log(obj2.name) // returns John, since delete will not traverse the prototype chain to delete keys

More info : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

about 4 years ago · Santiago Gelvez Relatório

0

There two points you should pay atention: 1- Object.create doesn't make copy of original object, it creates a new object with the argument object as prototype of the new object created. (prototype property is not configurable property) 2- delete operator just works on configurable properties of an object.

so, prototype properties are not deletable by delete operator, because they're not configurable.

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