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

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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