Back

Technologies:

javascriptjavascript
avatar
Tolerim
21 hours ago

The process of removing a property from an object is not functioning properly.

I am puzzled as to why the property company still exists under Emp1 even though I attempted to delete it. Here's what I did: I started by defining an object, Employee, with a company property. Then, I created a new object, Emp1, and attempted to delete the same property from it. However, even after the deletion, the property still appears under the Emp1 object. Can you explain why this is happening? For reference, here is the code snippet that I am using:

  var Employee = {
   company: 'xyz'
 }
 var Emp1 = Object.create(Employee);
 delete Emp1.company;
 console.log(Emp1.company);
 

 
Answers(1)
avatar
Tolerim
21 hours ago
Verified Answer
The reason why the property "company" still exists under the object Emp1 even after the delete operation is that the property is present in the prototype of the Emp1 object. When we create an object using Object.create() method and pass an object as a parameter, the passed object becomes the prototype of the newly created object. So, in this case, Emp1's prototype is the Employee object which still has the company property. Therefore, even after deleting the property from Emp1, it still exists in its prototype which is the Employee object. To delete the property completely, we can delete it from the prototype using the following code:
delete Employee.company;
;