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