RAVI TEJA NANI
RAVI TEJA NANI

Reputation: 11

Why deleted key in a object is still accessible in below example?

In the below given code, after deleting objA.foo, it is still accessible

var objA = Object.create({
  foo: 'foo'
});
var objB = objA;
objB.foo = 'bar';

delete objA.foo;
console.log(objA.foo);
console.log(objB.foo);

Upvotes: 1

Views: 42

Answers (1)

Pointy
Pointy

Reputation: 413826

You create object A with a prototype object that has a "foo" property. Then you add a local "foo" property to the object, and that's what's affected by the delete. (Note that object A and object B are the exact same object; the assignment does not make a copy.)

The subsequent references to the property "foo" are resolved at the prototype object, not object A itself.

Upvotes: 3

Related Questions