Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,919 --> 00:00:02,200
All right.
2
00:00:02,210 --> 00:00:04,059
Now let's add a new module
3
00:00:04,070 --> 00:00:05,079
to this application.
4
00:00:05,519 --> 00:00:07,460
So I'm going to add a new
5
00:00:07,469 --> 00:00:07,909
file
6
00:00:08,760 --> 00:00:10,340
logger dot
7
00:00:10,350 --> 00:00:10,989
Js.
8
00:00:11,359 --> 00:00:12,279
Let's imagine we're going
9
00:00:12,289 --> 00:00:14,140
to create a module for
10
00:00:14,149 --> 00:00:15,520
login messages and we're
11
00:00:15,529 --> 00:00:16,979
going to reuse this module
12
00:00:16,989 --> 00:00:18,059
in various parts of this
13
00:00:18,069 --> 00:00:19,530
application or potentially
14
00:00:19,540 --> 00:00:20,700
in other applications.
15
00:00:21,350 --> 00:00:23,170
So, logger dot Js.
16
00:00:24,559 --> 00:00:26,059
Now, in this module, let's
17
00:00:26,069 --> 00:00:27,120
imagine that we're going
18
00:00:27,129 --> 00:00:28,860
to use one of those remote
19
00:00:28,870 --> 00:00:30,739
logging services for
20
00:00:30,750 --> 00:00:32,130
logging our messages.
21
00:00:32,209 --> 00:00:33,680
So there are websites out
22
00:00:33,689 --> 00:00:35,220
there that provide logging
23
00:00:35,229 --> 00:00:36,900
as a service, they give us
24
00:00:36,909 --> 00:00:38,400
a URL and we can send an
25
00:00:38,409 --> 00:00:39,860
HTTP request to that
26
00:00:39,869 --> 00:00:41,220
URL to log
27
00:00:41,229 --> 00:00:42,900
messages in the cloud.
28
00:00:43,150 --> 00:00:44,950
So here I'm going to declare
29
00:00:45,159 --> 00:00:45,970
a variable
30
00:00:47,069 --> 00:00:47,959
like URL
31
00:00:49,150 --> 00:00:50,299
and set it to something like
32
00:00:50,310 --> 00:00:50,669
this.
33
00:00:50,680 --> 00:00:52,500
Http my
34
00:00:52,560 --> 00:00:54,090
logger dot io
35
00:00:54,099 --> 00:00:55,169
slash log.
36
00:00:55,639 --> 00:00:56,590
And of course, I'm making
37
00:00:56,599 --> 00:00:58,000
this up, it may not be a
38
00:00:58,009 --> 00:00:59,290
true service out there.
39
00:00:59,450 --> 00:01:00,930
But let's imagine in this
40
00:01:00,939 --> 00:01:01,990
implementation, we're going
41
00:01:02,000 --> 00:01:03,409
to send an HTTP
42
00:01:03,419 --> 00:01:05,410
request to this end point
43
00:01:05,419 --> 00:01:06,400
to this URL.
44
00:01:07,110 --> 00:01:08,569
Now, we also need a function
45
00:01:10,580 --> 00:01:12,510
called long a take a
46
00:01:12,519 --> 00:01:13,029
message.
47
00:01:14,550 --> 00:01:15,599
And in this function, we're
48
00:01:15,610 --> 00:01:17,489
going to send an HTTP
49
00:01:17,500 --> 00:01:18,080
request.
50
00:01:18,709 --> 00:01:20,580
However, to keep things
51
00:01:20,589 --> 00:01:21,769
simple here, we just want
52
00:01:21,779 --> 00:01:23,339
to focus on the modularity.
53
00:01:23,349 --> 00:01:24,580
We don't want to get distracted
54
00:01:24,589 --> 00:01:26,440
with all the details of sending
55
00:01:26,449 --> 00:01:27,750
HTTP request.
56
00:01:27,970 --> 00:01:29,250
So for now, I just want to
57
00:01:29,260 --> 00:01:31,019
log this message on the
58
00:01:31,029 --> 00:01:31,709
console.
59
00:01:31,989 --> 00:01:33,830
So console dot
60
00:01:33,839 --> 00:01:35,519
log message.
61
00:01:37,110 --> 00:01:37,610
OK.
62
00:01:38,349 --> 00:01:39,779
Now this
63
00:01:39,790 --> 00:01:41,470
variable and this log
64
00:01:41,489 --> 00:01:43,180
function, they're both
65
00:01:43,190 --> 00:01:44,949
scope to this module.
66
00:01:44,959 --> 00:01:46,410
They're private, they're
67
00:01:46,419 --> 00:01:48,029
not visible from the outside.
68
00:01:48,680 --> 00:01:50,209
However, in a
69
00:01:50,400 --> 00:01:52,339
OJs which is our main
70
00:01:52,349 --> 00:01:54,080
module, we want to use this
71
00:01:54,089 --> 00:01:55,199
log module.
72
00:01:55,330 --> 00:01:57,260
So we should be able
73
00:01:57,269 --> 00:01:58,949
to access this log
74
00:01:58,959 --> 00:01:59,639
function.
75
00:01:59,650 --> 00:02:00,889
We should be able to call
76
00:02:00,900 --> 00:02:02,220
it from the APP module.
77
00:02:02,449 --> 00:02:04,019
So we need to make this public,
78
00:02:04,080 --> 00:02:05,339
we need to make it visible
79
00:02:05,349 --> 00:02:06,099
from the outside.
80
00:02:07,559 --> 00:02:08,820
Now, in the last lecture,
81
00:02:09,070 --> 00:02:10,779
you saw this module object.
82
00:02:11,100 --> 00:02:12,179
One of the properties we
83
00:02:12,190 --> 00:02:13,500
have here is
84
00:02:13,509 --> 00:02:15,460
exports, you can see this
85
00:02:15,470 --> 00:02:16,979
property is set to an
86
00:02:16,990 --> 00:02:18,059
empty object.
87
00:02:18,789 --> 00:02:20,169
Anything that we add to this
88
00:02:20,179 --> 00:02:22,119
object will be exported from
89
00:02:22,130 --> 00:02:23,929
this module and it will be
90
00:02:23,940 --> 00:02:25,380
available outside of this
91
00:02:25,389 --> 00:02:25,839
module.
92
00:02:27,059 --> 00:02:28,660
So back in our logger module,
93
00:02:30,039 --> 00:02:31,350
I'm gonna set module
94
00:02:31,910 --> 00:02:33,039
dot exports
95
00:02:33,600 --> 00:02:34,899
dot log.
96
00:02:35,080 --> 00:02:36,960
So I'm adding a method called
97
00:02:36,970 --> 00:02:38,460
log to this
98
00:02:38,470 --> 00:02:39,860
exports object
99
00:02:40,270 --> 00:02:41,860
and simply setting it to
100
00:02:42,539 --> 00:02:43,880
this log function we have
101
00:02:43,889 --> 00:02:45,199
defined here.
102
00:02:45,940 --> 00:02:46,360
OK.
103
00:02:47,350 --> 00:02:48,570
In other words, the object
104
00:02:48,580 --> 00:02:49,830
that we're exporting here
105
00:02:50,029 --> 00:02:51,750
has a single method called
106
00:02:51,759 --> 00:02:52,440
lock.
107
00:02:52,800 --> 00:02:54,770
Now similarly, if you want
108
00:02:54,779 --> 00:02:56,690
to export this URL,
109
00:02:56,699 --> 00:02:57,559
we could do something like
110
00:02:57,570 --> 00:02:58,009
this.
111
00:02:58,429 --> 00:03:00,389
So module that
112
00:03:00,399 --> 00:03:01,869
exports that
113
00:03:01,880 --> 00:03:03,429
URL, we set it to
114
00:03:03,440 --> 00:03:05,309
URL and of course, we could
115
00:03:05,320 --> 00:03:06,750
change the name that is
116
00:03:06,759 --> 00:03:08,029
exported to the outside.
117
00:03:08,039 --> 00:03:09,059
For example,
118
00:03:09,070 --> 00:03:10,800
internally, we may call this
119
00:03:10,809 --> 00:03:12,179
variable URL.
120
00:03:12,630 --> 00:03:13,820
But when we export it, we
121
00:03:13,830 --> 00:03:14,550
may call it
122
00:03:15,619 --> 00:03:16,320
end point.
123
00:03:19,119 --> 00:03:19,550
OK.
124
00:03:20,199 --> 00:03:21,880
Now, in this case, we don't
125
00:03:21,889 --> 00:03:23,740
need to export this URL
126
00:03:23,750 --> 00:03:25,339
variable because this is
127
00:03:25,350 --> 00:03:27,259
purely implementation
128
00:03:27,270 --> 00:03:27,830
detail.
129
00:03:28,289 --> 00:03:29,869
So in real world applications,
130
00:03:29,899 --> 00:03:31,369
every module might have
131
00:03:31,380 --> 00:03:32,779
several variables and
132
00:03:32,789 --> 00:03:34,419
functions, we only want to
133
00:03:34,429 --> 00:03:35,990
export a subset of these
134
00:03:36,000 --> 00:03:37,720
members to the outside because
135
00:03:37,729 --> 00:03:39,139
we want to keep this module
136
00:03:39,149 --> 00:03:40,259
easy to use.
137
00:03:40,750 --> 00:03:42,059
Let me give you a metaphor,
138
00:03:42,509 --> 00:03:44,100
think of a DVD player.
139
00:03:44,520 --> 00:03:46,380
A DVD player has a few
140
00:03:46,389 --> 00:03:47,500
buttons on the outside.
141
00:03:47,509 --> 00:03:48,539
And these are the buttons
142
00:03:48,550 --> 00:03:50,100
or objects that we interact
143
00:03:50,110 --> 00:03:50,779
with.
144
00:03:50,830 --> 00:03:52,669
So these objects represent
145
00:03:52,759 --> 00:03:54,589
the public interface
146
00:03:54,690 --> 00:03:56,070
of a DVD player.
147
00:03:56,369 --> 00:03:56,899
OK?
148
00:03:57,199 --> 00:03:58,649
But inside the box, there
149
00:03:58,660 --> 00:04:00,199
are lots of other objects
150
00:04:00,210 --> 00:04:01,490
or complex objects.
151
00:04:01,500 --> 00:04:03,020
We don't need to know anything
152
00:04:03,029 --> 00:04:04,460
about these objects, their
153
00:04:04,470 --> 00:04:06,199
implementation detail and
154
00:04:06,210 --> 00:04:07,720
they can change significantly
155
00:04:07,729 --> 00:04:09,330
from one model to another.
156
00:04:09,690 --> 00:04:11,229
But what we see on the outside
157
00:04:11,240 --> 00:04:13,139
is almost stable or
158
00:04:13,149 --> 00:04:14,960
static across different
159
00:04:14,970 --> 00:04:15,600
models.
160
00:04:15,789 --> 00:04:17,470
So in our logger module,
161
00:04:17,690 --> 00:04:19,089
this URL is
162
00:04:19,100 --> 00:04:20,428
implementation detail.
163
00:04:20,570 --> 00:04:21,988
Other modules don't need
164
00:04:22,000 --> 00:04:23,309
to know anything about this.
165
00:04:23,320 --> 00:04:24,980
They only need to call the
166
00:04:24,989 --> 00:04:25,790
log function.
167
00:04:26,140 --> 00:04:27,630
So we export this, make it
168
00:04:27,640 --> 00:04:29,470
public but keep the URL
169
00:04:29,480 --> 00:04:30,149
private.
170
00:04:30,339 --> 00:04:31,929
So I'm going to delete this
171
00:04:31,940 --> 00:04:32,630
last line.
172
00:04:33,200 --> 00:04:33,750
OK.
173
00:04:33,910 --> 00:04:35,070
So we are done with our logger
174
00:04:35,079 --> 00:04:35,709
module.
175
00:04:35,929 --> 00:04:37,119
Now we need to load this
176
00:04:37,130 --> 00:04:38,519
module and use it
177
00:04:38,720 --> 00:04:40,630
inside AJ
178
00:04:40,640 --> 00:04:40,970
S
9985
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.