Actually this idea of pointing is slightly wrong. There is even that, but let’s start at the end that you understand better.
When you assign 23 to the variable a
is very easy to understand. You are placing a numeric object whose value is 23 in the storage location with a name called a
.
If you don’t understand that you need to read What is a variable?.
And if you put the number 12 in a
?
Is putting a another numeric object whose value is 12. Noted the highlight?
I think we can understand that the previous value is discarded.
You can do it all you want.
And in Javascript you can put objects of other types because it’s a dynamic typing language, so only objects have types, the variable does not.
What happens if you do as at the beginning and now assign a array for a
? What will be stored in a
?
Types by value X type by reference
What I showed initially is the type by value that is easier to understand. The object goes straight into the variable.
It turns out that there are types by reference. They are types that depend on indirect. So you have a reference which is a pointer to another object and in another different place has the actual object.
This is necessary because this object has a size to be defined, there is no way to reserve memory previously in the variable’s storage location. Even if I knew how big it could be.
What is the value that goes in the variable? The value of the pointer, which is a memory address to where the object actually is, then enters the pointer.
When a
is worth 23 and you do:
b = a
you are copying the numeric object that has the value 23 that is in a
to the site of b
. You will have two variables as the same value, although they are different objects, physically there are two. Simple.
But what you’re copying when you have an object is by reference to array?
It’s copying the reference, the pointer. Then you have two variables with the same value, a memory address pointing to the same object. This you can see. When you changed the composition of the object by one variable the object changed by the other because it is the same object. Two pointers but only one object.
But what happened when you did this?
a = [1, 2, 3] //isto é um literal de criação de array
You just created an object new and assigns to a
the address to her. And what happened to b
? Kept pointing at the object he was pointing at before, no one touched b
.
So now you have a
pointing to an object that has just been created and b
pointing to another object that already existed before. No longer has two variables pointing to the same object.
Noted that the value that was in a
what was the reference for the first object was discarded? It happened equal to the type by value 23
, it has been discarded. Not necessarily the pointed object. In fact the object still exists because b
continues pointing to it. If no one pointed to this object but it would be discarded as well. This is the type mechanism by reference.
If the goal was to understand memory management of how this happens the question would be another.
Completion
So that’s it, in types by value you always create new objects when you assign something to the variable, in types by reference only in some situations it happens, only creates a new object take off create a new object. Assign b = a
in a reference type is not creating a new object, only when you use a syntax that is clearly a new object literal is you creating something new.
Thank you very much, I couldn’t have given you a better explanation!
– Gustavo Paiva
Related/complementary: https://answall.com/questions/2695/howto function
– bfavaretto