I know this works, but I don't know why or the reasoning behind why it was made to work this way:
var foo = [5, 10];
var bar = foo;
console.log(foo); //[5, 10]
console.log(bar); //[5, 10]
bar[0] = 1;
console.log(foo); //[1, 10]
bar = null;
console.log(foo); //[1, 10]
I would have expected not just bar
to become null, but foo
as well. I'd love some help understanding this.
Reference, but it's definition is just a pointer to some object. When you assign
null
to bar, you simply delete this "pointer" that was stored inbar
, making it no longer point to object.Imagine this as an address, written on sheets of paper. If sheet
foo
andbar
have same address on it and you issue a command: go to address written on sheetfoo
and put something in house there, after operation is done, naturally, whoever looks at any of those sheets and checks out that address, will find those changes. Now your assignment ofnull
is just same as wiping this address from sheet with eraser. It doesn't touch neither any other sheets, nor house in any way.When you do
It creates a reference to an array, like this
When you assign
bar
tofoo
, you now haveWhen you call
you get
where
foo
andbar
point to the same object.When you call
you now have
The difference is between rebinding and mutating operations.
is mutating; it affects the object that
bar
points to.is rebinding; it just affects what the identifier
bar
means.You have two pointers to the array. Bar = foo copies the original pointer. Dereferencing bar does not dereference foo.
Read more @ http://snook.ca/archives/javascript/javascript_pass
I like to think of variables as labels. Makes explaining them easier.
var foo = [5, 10]; var bar = foo; console.log(foo); //[5, 10] console.log(bar); //[5, 10] bar[0] = 1; console.log(foo); //[1, 10] bar = null; console.log(foo); //[1, 10]
When you do
bar = null;
that just assigns something to the value of thebar
variable. It does not affect what used to be assigned tobar
. That object continues to live on and if there are other references to it, it stays alive with its value untouched.When you do this:
You have three entities. You have an array
[5,10]
and two variables each with a reference to that array. If you change the array, then since both variables point to the same array, you will see that change no matter which variable you reference the array through.But, if you set
bar = null
, that just affects thebar
variable which then no longer has a reference to the array. It doesn't affect the array at all which is still pointed to byfoo
.In fact, if you did this:
You'd have the same sort of result. After the second line of code both
bar
andfoo
pointed to the same array, but after the third line,bar
now points to a new array and onlyfoo
points to the original array. The key is realizing that there's a difference between usingbar
to modify the object it points to as inbar[0] = 1
versus reassigning the whole value of bar as inbar = [20,30]
. In the first case, the underlying object that bothfoo
andbar
point to is changed. In the second case, the underlying object thatbar
originally pointed to is not touched. Instead,bar
is changed to point a new object and the prior object is not touched.