In this video, I'm going to show you one more distinction between objects and primitives, in some
ways their behavior is different.
Let's suppose that we have a variable AI which has the value ten.
So when the variable AI is created, JavaScript engine in memory creates new spot for this variable
and saves the value of a certain.
Then again, suppose that we have variable B, which equals to eight, in this case, JavaScript engine
creates different spot in memory for variable B and copies the value of A, which is 10.
So you see that we have two different variables, A and B in memory, we have two different spots for
them and both variables have the same value.
After that, if we change the value of variable A and assigned to it 20, then it will change its value
into twenty.
But the variable B will remain its value as ten makes sense, right?
It's not special.
I think it's obvious.
But something different happens when we deal with objects.
Let's say we have objects C which has property name with value, John.
So when this object is created, javascript engine in memory creates new memory space for it and variable
C is just a reference for this object.
It means that variable C knows where the object is located in memory.
OK, let's suppose that we have a variable deal, which equals two variable C in this case for variable
D, there is not created a new spot in memory space as it was in case of primitives.
The fact is that the variable D as variable C becomes just one more reference to the existing object.
So it's something different, right.
In case of primitives, variables copied the values, but in case of objects, variable D did not copy
the value of verbal C, it just became the reference for the existing object.
Of course, if we change the value of name property, John will be replaced with Nick and if we check
values of nine properties for ACMD, it will be the exact same.
We will get Nick in both cases.
OK, let's open our text editor and recall the first example regarding primitives, so we had variable
AI, which was equal to 10, so right.
VA equals to ten and then VA p equals to a.
Below, right, a equals 220, let's lock them in council, right, consultor like a.
And then cancel that Lockerby.
Remote.
So he would have different values for A and B, 20 and 10, and if we check global window object right
window don't castle.
Then drop down it.
You see that we have variable rate and variable B with different values.
So again, they took different spots in memory.
And because of that, when we change the value of a it didn't have an effect on the value of variable
B.
OK, now let's create object right for sequel's.
The right name, property and the value, John.
Below right variable D equals to C.
Reload and then check again, window object.
So you see that we have here variable C and below, we have variable as well.
They have the same values for name property, which is John.
It seems that they look like as different objects.
But actually it's not right.
In order to prove that I'm going to change the value of object C or D doesn't matter.
That's right.
C dot name equals unique.
Then reload and check again.
We don't object right window.
And dropped down eight.
You see that both of them have changed the values in Tenwick, it means that they are not different.
We have just one object in memory and see and they are just references of that object.
Makes sense.
All right, let's try something different instead of documentation, let's use equal sign to change
the value of any property.
So write sequels.
And write your name.
Disvalue, Bob.
Reload.
And right again, rindo.
So you see that variable C has the value of name property as Bob and Variable D still has value as Nick.
It means that when we used equals sine we have created a new object which to different spot in memory.
OK, let's see exactly what happened.
So when the C variable was created, JavaScript Engine created a new spot in memory for that object.
Then we had a variable D which was equal to C and they both were references for the same object.
Then using dot operator, we have changed the value of name property in Tenwick, so nothing special
happened.
CI a. still referenced to the same object, but when we used equal sign and changed the name into that
new object was created which took new spot in memory space.
So D was remained as a reference for a previous object and C became a reference for newly created object.
That's why didn't it change the content of the.
All right, that's the way how objects and primitives act, I think it was interesting and clear for
you.
See you in the next video.