pyronaur
pyronaur

Reputation: 3545

Deleting original object while preserving a copy of it in JavaScript

I tested how JavaScript Objects behave in Chrome Console. Is it safe to assume this will work anywhere ? :

a = new Object()
>> Object {}
b = a
>> Object {}
a.boo = "Yiss!"
>> "Yiss!"
b
>> Object {boo: "Yiss!"}
a = new Object()
>> Object {}
b
>> Object {boo: "Yiss!"}
a = b
>> Object {boo: "Yiss!"}
delete a.boo
>> true
b
>> Object {}
a
>> Object {}
delete a
>> true
a
>> Uncaught ReferenceError: a is not defined
b
>> Object {}

TL;DR

I just want to be clear about if the following statement is true: Modifications made in 1 of many copies of an object are available on all copies of that object. If a copy or the original is deleted or replaced by a new object, it doesn't impact other copies.

Upvotes: 0

Views: 1638

Answers (3)

apsillers
apsillers

Reputation: 115970

This behavior is completely normal and should be expected from all JavaScript environments. Let's walk through it step by step.

Create a new object and store it in a:

a = new Object()
>> Object {}

Make b refer to the same single object that b does:

b = a
>> Object {}

(You seem to think this "copies" the object somehow. This is not correct: there is still only one object so far, but you can refer to it with a and b. You've given one single object two different variable names.)

Modify the object referred to by a (remember this is the same single object referred to by b as well) to have a boo property:

a.boo = "Yiss!"
>> "Yiss!"

Confirm that object referred to by b (which is the same as the one referred to by a) has changed:

b
>> Object {boo: "Yiss!"}

Make a refer to a new object:

a = new Object()
>> Object {}

b still refers to the first object:

b
>> Object {boo: "Yiss!"}

Make a refer to the original object again:

a = b
>> Object {boo: "Yiss!"}

Remove the boo property from the original object:

delete a.boo
>> true

Confirm that the boo property has been removed from the object referred to by b (which is the same as the object referred to by a):

b
>> Object {}

Confirm that the boo property has been removed from the object referred to by a (which is the same as the object referred to by b):

a
>> Object {}

Remove the variable name "a" from the global namespace:

delete a
>> true

The variable name "a" no longer exists:

a
>> Uncaught ReferenceError: a is not defined

The variable b still refers to the original object:

b
>> Object {}

You seem to think delete destroys values. It does not. Instead, it removes properties from objects. Since global variables are properties on the global object, they can be removed as well (i.e., here, a is the same as window.a).

Upvotes: 1

Ginden
Ginden

Reputation: 5316

Objects in JavaScript are [almost] always passed by reference. In your case, a and b are the same object.

delete operator does not delete objects or variables, but properties. In your example, you define a and b in global scope - so they are treated as window properties. It's only reason behind delete working as you expect.

Upvotes: 0

cdhowie
cdhowie

Reputation: 169173

All that delete does is remove a property from an object. It does not actually do anything to the value of the property.

a and b here are equivalent to window.a and window.b because they are global, and window is the global object. delete a is therefore equivalent to delete window.a which simply removes the a property from the window object.

Since variables in JavaScript don't store objects but rather references to objects, all that delete a does is remove a variable that contains a reference to the same object that continues to be referenced by b.

So, your question itself is flawed because it is based on a false understanding of object semantics in JavaScript:

If a copy or the original is deleted or replaced by a new object, it doesn't impact other copies.

There is no such this as a "copy" or an "original" in this context. The object is not being copied at all. You simply have one object that is referenced by multiple variables. And, further, there is no concept of explicitly "deleting an object" in JavaScript.

Upvotes: 4

Related Questions