WEBVTT

00:01.110 --> 00:06.690
In this video, I'm going to show you one more distinction between objects and primitives, in some

00:06.690 --> 00:08.410
ways their behavior is different.

00:09.390 --> 00:13.000
Let's suppose that we have a variable AI which has the value ten.

00:13.740 --> 00:20.790
So when the variable AI is created, JavaScript engine in memory creates new spot for this variable

00:21.330 --> 00:23.640
and saves the value of a certain.

00:25.220 --> 00:32.630
Then again, suppose that we have variable B, which equals to eight, in this case, JavaScript engine

00:32.630 --> 00:39.740
creates different spot in memory for variable B and copies the value of A, which is 10.

00:41.060 --> 00:48.110
So you see that we have two different variables, A and B in memory, we have two different spots for

00:48.110 --> 00:50.980
them and both variables have the same value.

00:52.640 --> 01:00.560
After that, if we change the value of variable A and assigned to it 20, then it will change its value

01:00.560 --> 01:01.340
into twenty.

01:01.490 --> 01:08.330
But the variable B will remain its value as ten makes sense, right?

01:09.650 --> 01:10.570
It's not special.

01:10.880 --> 01:12.170
I think it's obvious.

01:13.520 --> 01:16.430
But something different happens when we deal with objects.

01:17.420 --> 01:21.730
Let's say we have objects C which has property name with value, John.

01:22.400 --> 01:30.320
So when this object is created, javascript engine in memory creates new memory space for it and variable

01:30.320 --> 01:32.830
C is just a reference for this object.

01:33.590 --> 01:38.090
It means that variable C knows where the object is located in memory.

01:39.250 --> 01:46.690
OK, let's suppose that we have a variable deal, which equals two variable C in this case for variable

01:46.690 --> 01:52.310
D, there is not created a new spot in memory space as it was in case of primitives.

01:52.960 --> 02:00.300
The fact is that the variable D as variable C becomes just one more reference to the existing object.

02:01.570 --> 02:03.510
So it's something different, right.

02:04.030 --> 02:11.350
In case of primitives, variables copied the values, but in case of objects, variable D did not copy

02:11.350 --> 02:16.300
the value of verbal C, it just became the reference for the existing object.

02:17.850 --> 02:24.960
Of course, if we change the value of name property, John will be replaced with Nick and if we check

02:24.960 --> 02:29.490
values of nine properties for ACMD, it will be the exact same.

02:30.210 --> 02:32.190
We will get Nick in both cases.

02:33.930 --> 02:42.660
OK, let's open our text editor and recall the first example regarding primitives, so we had variable

02:42.660 --> 02:44.820
AI, which was equal to 10, so right.

02:44.820 --> 02:50.400
VA equals to ten and then VA p equals to a.

02:53.240 --> 02:59.780
Below, right, a equals 220, let's lock them in council, right, consultor like a.

03:02.050 --> 03:03.700
And then cancel that Lockerby.

03:06.460 --> 03:07.000
Remote.

03:08.370 --> 03:17.220
So he would have different values for A and B, 20 and 10, and if we check global window object right

03:17.220 --> 03:18.210
window don't castle.

03:20.390 --> 03:21.530
Then drop down it.

03:23.510 --> 03:29.030
You see that we have variable rate and variable B with different values.

03:29.720 --> 03:32.840
So again, they took different spots in memory.

03:33.230 --> 03:39.050
And because of that, when we change the value of a it didn't have an effect on the value of variable

03:39.050 --> 03:39.290
B.

03:40.740 --> 03:44.970
OK, now let's create object right for sequel's.

03:46.140 --> 03:49.440
The right name, property and the value, John.

03:51.380 --> 03:54.590
Below right variable D equals to C.

03:58.190 --> 04:01.670
Reload and then check again, window object.

04:06.050 --> 04:12.620
So you see that we have here variable C and below, we have variable as well.

04:13.490 --> 04:17.110
They have the same values for name property, which is John.

04:18.620 --> 04:21.500
It seems that they look like as different objects.

04:21.500 --> 04:22.940
But actually it's not right.

04:23.570 --> 04:29.090
In order to prove that I'm going to change the value of object C or D doesn't matter.

04:29.510 --> 04:30.190
That's right.

04:30.200 --> 04:33.530
C dot name equals unique.

04:36.830 --> 04:39.600
Then reload and check again.

04:39.620 --> 04:41.390
We don't object right window.

04:43.400 --> 04:44.660
And dropped down eight.

04:47.550 --> 04:54.230
You see that both of them have changed the values in Tenwick, it means that they are not different.

04:54.660 --> 05:00.300
We have just one object in memory and see and they are just references of that object.

05:01.680 --> 05:02.300
Makes sense.

05:03.690 --> 05:11.160
All right, let's try something different instead of documentation, let's use equal sign to change

05:11.160 --> 05:12.510
the value of any property.

05:12.600 --> 05:14.160
So write sequels.

05:16.460 --> 05:17.840
And write your name.

05:19.480 --> 05:20.290
Disvalue, Bob.

05:23.520 --> 05:24.120
Reload.

05:25.860 --> 05:27.270
And right again, rindo.

05:31.350 --> 05:40.720
So you see that variable C has the value of name property as Bob and Variable D still has value as Nick.

05:41.580 --> 05:49.470
It means that when we used equals sine we have created a new object which to different spot in memory.

05:50.700 --> 05:53.760
OK, let's see exactly what happened.

05:54.480 --> 06:01.500
So when the C variable was created, JavaScript Engine created a new spot in memory for that object.

06:02.540 --> 06:09.690
Then we had a variable D which was equal to C and they both were references for the same object.

06:11.200 --> 06:18.760
Then using dot operator, we have changed the value of name property in Tenwick, so nothing special

06:18.760 --> 06:19.150
happened.

06:19.570 --> 06:28.660
CI a. still referenced to the same object, but when we used equal sign and changed the name into that

06:28.660 --> 06:33.190
new object was created which took new spot in memory space.

06:33.850 --> 06:40.930
So D was remained as a reference for a previous object and C became a reference for newly created object.

06:42.170 --> 06:45.530
That's why didn't it change the content of the.

06:48.220 --> 06:55.840
All right, that's the way how objects and primitives act, I think it was interesting and clear for

06:55.840 --> 06:56.040
you.

06:56.650 --> 06:57.820
See you in the next video.
