#Module Exercise
Here, we will build an inline module like the following step-by-step:
var ourModule = (function(arg, transform) {
// secret internals
var value = arg;
// exports
return function() {
// has access to secret internals!
value = transform(value);
return value;
};
})(something);
This repository features a solved
branch for future reference.
##Step 1 Once you've forked and cloned this repo, you're ready to start the exercise.
- Open
index.js
. - Look at the code and ask questions if anything looks foreign.
- Leave
f
as-is or alter it to create a more interesting module. - Leave the statements toward the end of the document as-is or alter them to do something more interesting.
- Run the script with
npm start
ornode index.js
.
##Step 2
- Open
index.js
. - Put a pair of parens (
(
and)
) around our declaration off
.
Recall that the function declaration begins with the keyword function
and
ends with the closing curly brace (}
) following the function body. Your
opening paren belongs before the letter 'f' in function
and your closing paren
belongs after }
.
- Observe that this makes no actual change in what the code does. Surrounding a value with parens does nothing to change it. Run the script again if you like.
##Step 3
Since we named our function f
, we know that the f
being called where we
define ourModule
is the same function, right? We can replace f
with the
actual function declaration, then, and it'll be the same, won't it?
- Copy the declaration of
f
inindex.js
, with the parens that surround it. - Substitute the declaration you copied for
f
in the line where we declare and define the variableourModule
. - Remove the declaration of
f
from the lines above our declaration ofourModule
. - Run the script again and observe any difference in the result.
##Step 4
- Look at the code you've created to generate and store a module in
ourModule
. - Look at the code at the top of this page.