Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right.
Now let's add a new module
to this application.
So I'm going to add a new
file
logger dot
Js.
Let's imagine we're going
to create a module for
login messages and we're
going to reuse this module
in various parts of this
application or potentially
in other applications.
So, logger dot Js.
Now, in this module, let's
imagine that we're going
to use one of those remote
logging services for
logging our messages.
So there are websites out
there that provide logging
as a service, they give us
a URL and we can send an
HTTP request to that
URL to log
messages in the cloud.
So here I'm going to declare
a variable
like URL
and set it to something like
this.
Http my
logger dot io
slash log.
And of course, I'm making
this up, it may not be a
true service out there.
But let's imagine in this
implementation, we're going
to send an HTTP
request to this end point
to this URL.
Now, we also need a function
called long a take a
message.
And in this function, we're
going to send an HTTP
request.
However, to keep things
simple here, we just want
to focus on the modularity.
We don't want to get distracted
with all the details of sending
HTTP request.
So for now, I just want to
log this message on the
console.
So console dot
log message.
OK.
Now this
variable and this log
function, they're both
scope to this module.
They're private, they're
not visible from the outside.
However, in a
OJs which is our main
module, we want to use this
log module.
So we should be able
to access this log
function.
We should be able to call
it from the APP module.
So we need to make this public,
we need to make it visible
from the outside.
Now, in the last lecture,
you saw this module object.
One of the properties we
have here is
exports, you can see this
property is set to an
empty object.
Anything that we add to this
object will be exported from
this module and it will be
available outside of this
module.
So back in our logger module,
I'm gonna set module
dot exports
dot log.
So I'm adding a method called
log to this
exports object
and simply setting it to
this log function we have
defined here.
OK.
In other words, the object
that we're exporting here
has a single method called
lock.
Now similarly, if you want
to export this URL,
we could do something like
this.
So module that
exports that
URL, we set it to
URL and of course, we could
change the name that is
exported to the outside.
For example,
internally, we may call this
variable URL.
But when we export it, we
may call it
end point.
OK.
Now, in this case, we don't
need to export this URL
variable because this is
purely implementation
detail.
So in real world applications,
every module might have
several variables and
functions, we only want to
export a subset of these
members to the outside because
we want to keep this module
easy to use.
Let me give you a metaphor,
think of a DVD player.
A DVD player has a few
buttons on the outside.
And these are the buttons
or objects that we interact
with.
So these objects represent
the public interface
of a DVD player.
OK?
But inside the box, there
are lots of other objects
or complex objects.
We don't need to know anything
about these objects, their
implementation detail and
they can change significantly
from one model to another.
But what we see on the outside
is almost stable or
static across different
models.
So in our logger module,
this URL is
implementation detail.
Other modules don't need
to know anything about this.
They only need to call the
log function.
So we export this, make it
public but keep the URL
private.
So I'm going to delete this
last line.
OK.
So we are done with our logger
module.
Now we need to load this
module and use it
inside AJ
S
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.