Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:01,430 --> 00:00:04,900
In the last lecture we implemented a new class
2
2
00:00:04,900 --> 00:00:09,660
which showed us the need for encapsulation and data privacy.
3
3
00:00:09,660 --> 00:00:13,330
So let's now tackle this very important principle
4
4
00:00:13,330 --> 00:00:15,313
of object oriented programming.
5
5
00:00:17,150 --> 00:00:21,350
Now first, remember that encapsulation basically means
6
6
00:00:21,350 --> 00:00:25,990
to keep some properties and methods private inside the class
7
7
00:00:25,990 --> 00:00:30,500
so that they are not accessible from outside of the class.
8
8
00:00:30,500 --> 00:00:34,110
Then the rest of the methods are basically exposed
9
9
00:00:34,110 --> 00:00:38,530
as a public interface, which we can also call API.
10
10
00:00:38,530 --> 00:00:41,380
So this is essential to do in anything
11
11
00:00:41,380 --> 00:00:44,000
more than a toy application.
12
12
00:00:44,000 --> 00:00:47,930
Now, there are two big reasons why we need encapsulation
13
13
00:00:47,930 --> 00:00:49,650
and data privacy.
14
14
00:00:49,650 --> 00:00:52,290
So first it is to prevent code
15
15
00:00:52,290 --> 00:00:56,070
from outside of a class to accidentally manipulate
16
16
00:00:56,070 --> 00:00:58,740
or data inside the class.
17
17
00:00:58,740 --> 00:01:00,570
And that's exactly what we did
18
18
00:01:00,570 --> 00:01:03,680
by the end of the last lecture here.
19
19
00:01:03,680 --> 00:01:05,310
So this is also the reason why
20
20
00:01:05,310 --> 00:01:08,080
we implement a public interface.
21
21
00:01:08,080 --> 00:01:12,070
So we are not supposed to manually mess with this property
22
22
00:01:12,070 --> 00:01:14,203
and therefore we should encapsulate it.
23
23
00:01:15,140 --> 00:01:16,460
Now, the second reason is
24
24
00:01:16,460 --> 00:01:20,000
that when we expose only a small interface
25
25
00:01:20,000 --> 00:01:24,920
so a small API consisting only of a few public methods
26
26
00:01:24,920 --> 00:01:28,250
then we can change all the other internal methods
27
27
00:01:28,250 --> 00:01:29,820
with more confidence.
28
28
00:01:29,820 --> 00:01:32,240
Because in this case, we can be sure
29
29
00:01:32,240 --> 00:01:36,910
that external code does not rely on these private methods.
30
30
00:01:36,910 --> 00:01:39,750
And so therefore our code will not break
31
31
00:01:39,750 --> 00:01:42,110
when we do internal changes.
32
32
00:01:42,110 --> 00:01:43,930
So that's what encapsulation
33
33
00:01:43,930 --> 00:01:47,290
and data privacy are and the reasons for it.
34
34
00:01:47,290 --> 00:01:49,493
And so let's now actually implement it.
35
35
00:01:50,350 --> 00:01:52,940
However JavaScript classes actually
36
36
00:01:52,940 --> 00:01:57,740
do not yet support real data privacy and encapsulation.
37
37
00:01:57,740 --> 00:02:01,740
Now there is a proposal to add truly private class fields
38
38
00:02:01,740 --> 00:02:03,800
and methods to the language,
39
39
00:02:03,800 --> 00:02:06,300
but it's not completely ready yet.
40
40
00:02:06,300 --> 00:02:09,020
I will still show it to you in the next lecture
41
41
00:02:09,020 --> 00:02:10,800
because it's really cool
42
42
00:02:10,800 --> 00:02:13,940
but even when this feature ships in the browser
43
43
00:02:13,940 --> 00:02:17,870
it will take some time until you can use it with confidence.
44
44
00:02:17,870 --> 00:02:22,400
And so in this lecture, we will basically fake encapsulation
45
45
00:02:22,400 --> 00:02:25,460
by simply using a convention.
46
46
00:02:25,460 --> 00:02:29,220
So the first candidate to protect here is again,
47
47
00:02:29,220 --> 00:02:32,930
this movements array that we have been talking about.
48
48
00:02:32,930 --> 00:02:35,600
So the movements are mission critical data
49
49
00:02:35,600 --> 00:02:38,180
and so here we will protect this data
50
50
00:02:38,180 --> 00:02:40,913
so that no one can accidentally manipulate it.
51
51
00:02:41,840 --> 00:02:45,700
And for now, all we will do is to add this underscore
52
52
00:02:45,700 --> 00:02:49,353
in front of the property name and that's it.
53
53
00:02:50,350 --> 00:02:53,373
Now we need to then go ahead and change it everywhere.
54
54
00:02:54,770 --> 00:02:59,770
So that's here and yeah, that's actually it.
55
55
00:03:00,020 --> 00:03:03,290
So again this does not actually make
56
56
00:03:03,290 --> 00:03:05,570
the property truly private
57
57
00:03:05,570 --> 00:03:08,430
because this is just a convention.
58
58
00:03:08,430 --> 00:03:12,180
So it's something that developers agree to use
59
59
00:03:12,180 --> 00:03:14,910
and then everyone does it this way.
60
60
00:03:14,910 --> 00:03:19,910
But since it is not truly private we call this protected,
61
61
00:03:21,390 --> 00:03:24,683
so protected property.
62
62
00:03:26,630 --> 00:03:31,470
And so now if we wanted to get the movements outside here
63
63
00:03:31,470 --> 00:03:34,220
we could, of course still do this.
64
64
00:03:34,220 --> 00:03:36,040
So that's what I was just saying
65
65
00:03:37,210 --> 00:03:42,170
which is that the data here is of course still accessible
66
66
00:03:42,170 --> 00:03:46,750
if we use this underscore outside here as well
67
67
00:03:46,750 --> 00:03:49,230
but at least now everyone on your team
68
68
00:03:49,230 --> 00:03:52,730
and that includes yourself will know that this property
69
69
00:03:52,730 --> 00:03:56,900
is not supposed to be touched outside of the class.
70
70
00:03:56,900 --> 00:03:58,520
So you can still do it
71
71
00:03:58,520 --> 00:04:02,173
but at least you will know that it is wrong, okay?
72
72
00:04:03,110 --> 00:04:06,040
Now, if we still wanted to give access
73
73
00:04:06,040 --> 00:04:08,850
to the movements array from the outside
74
74
00:04:08,850 --> 00:04:12,573
then we would have to implement a public method for that.
75
75
00:04:13,920 --> 00:04:16,033
So let's do that actually right here.
76
76
00:04:17,060 --> 00:04:20,930
So let's say get movements.
77
77
00:04:20,930 --> 00:04:24,240
And of course we could also create a getter here
78
78
00:04:24,240 --> 00:04:28,730
instead of this method, but let's just keep it simple.
79
79
00:04:28,730 --> 00:04:31,610
So it's very common to actually have a method
80
80
00:04:31,610 --> 00:04:36,200
that is called get or set instead of using a real getter
81
81
00:04:36,200 --> 00:04:37,183
or a real setter.
82
82
00:04:38,480 --> 00:04:40,030
And so all that this will do
83
83
00:04:40,030 --> 00:04:44,603
is to return this.movements.
84
84
00:04:45,870 --> 00:04:47,480
And so now this would be
85
85
00:04:47,480 --> 00:04:52,480
the correct way to get the movements, so get movements.
86
86
00:04:57,150 --> 00:04:59,060
And so this one everyone
87
87
00:04:59,060 --> 00:05:01,740
can still at least access the movements
88
88
00:05:01,740 --> 00:05:03,970
but they cannot override them.
89
89
00:05:03,970 --> 00:05:06,550
So they cannot set the movements
90
90
00:05:06,550 --> 00:05:11,190
unless of course they use the underscore with the convention
91
91
00:05:11,190 --> 00:05:12,500
but then at least they will know
92
92
00:05:12,500 --> 00:05:15,500
that it's wrong to access the property.
93
93
00:05:15,500 --> 00:05:19,923
Next, we could also protect here the pin.
94
94
00:05:21,060 --> 00:05:24,160
So this is also something that doesn't make much sense
95
95
00:05:24,160 --> 00:05:26,980
to be accessible from the outside.
96
96
00:05:26,980 --> 00:05:29,917
And in fact, we could make everything protected here
97
97
00:05:29,917 --> 00:05:34,040
but I think that like this, we are good to go.
98
98
00:05:34,040 --> 00:05:36,800
So this is just a small example anyway.
99
99
00:05:36,800 --> 00:05:39,173
And so for now, let's leave it like this.
100
100
00:05:40,090 --> 00:05:42,950
To finish, let's just also protect
101
101
00:05:42,950 --> 00:05:46,140
then the approved loan method.
102
102
00:05:46,140 --> 00:05:48,650
So this one I also mentioned by the end
103
103
00:05:48,650 --> 00:05:50,750
of the previous lecture.
104
104
00:05:50,750 --> 00:05:53,410
So this is a method that is only
105
105
00:05:53,410 --> 00:05:56,400
supposed to be internally by the bank.
106
106
00:05:56,400 --> 00:05:58,920
Let's say basically just to check
107
107
00:05:58,920 --> 00:06:01,200
if a loan should be approved.
108
108
00:06:01,200 --> 00:06:04,630
So essentially this method here should not be part
109
109
00:06:04,630 --> 00:06:08,653
of the public API, but all the others should be.
110
110
00:06:09,660 --> 00:06:11,890
So right now we are protecting this method.
111
111
00:06:11,890 --> 00:06:14,960
And so our public API right now consists
112
112
00:06:14,960 --> 00:06:18,823
of these other four remaining methods, all right?
113
113
00:06:20,160 --> 00:06:24,730
So this is how we protect fields from unwanted access.
114
114
00:06:24,730 --> 00:06:27,860
Now, as I said, of course, developers need to know
115
115
00:06:27,860 --> 00:06:31,090
about this convention and need to follow it
116
116
00:06:31,090 --> 00:06:34,940
because otherwise everything will still be public.
117
117
00:06:34,940 --> 00:06:37,800
But now in the next lecture, we're actually gonna talk
118
118
00:06:37,800 --> 00:06:41,500
about truly private fields and methods.
119
119
00:06:41,500 --> 00:06:45,163
And so with that, we will then fix this problem for good.
10564
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.