Skip to content

Commit ba0b671

Browse files
committed
More module stuff
1 parent 369000b commit ba0b671

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎chapters/modules.md‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Next we will look at utilizing out new module in other libraries.
3434

3535
Create a second file named _./app.js_ with the code shown below. First we load in the core `sys` module, which provides common methods such as `sys.puts()`, `sys.print()`, and the object inspection method `sys.p()`.
3636

37-
3837
var sys = require('sys'),
3938
utils = require('./utilities');
4039

@@ -43,4 +42,34 @@ Create a second file named _./app.js_ with the code shown below. First we load i
4342
utils.merge(a, b);
4443
sys.p(a);
4544

46-
Since the `sys` module is bundled with node, it's `exports` are returned, however for other modules node will iterate the `require.paths` array in search of a module matching the given path. By default `require.paths` includes _~/.node_libraries_, so if we have _~/.node_libraries_/utilities.js_ we may simply `require('utilities')`, instead of our relative example `require('./utilities')` shown above.
45+
Since the `sys` module is bundled with node, it's `exports` are returned, however for other modules node will iterate the `require.paths` array in search of a module matching the given path. By default `require.paths` includes _~/.node_libraries_, so if we have _~/.node_libraries_/utilities.js_ we may simply `require('utilities')`, instead of our relative example `require('./utilities')` shown above.
46+
47+
Node also supports the idea of _index_ JavaScript files, to illustrate this example lets create a _math_ module that will provide the `math.add()`, and `math.sub()` methods. For organizational purposes we will keep each method in their respective _./math/add.js_ and _./math/sub.js_ files. So where does _index.js_ come into play? we can populate _./math/index.js_ with the code shown below, which is used when `require('./math')` is invoked, which is conceptually identical to invoking `require('./math/index')`.
48+
49+
exports = {
50+
add: require('./add'),
51+
sub: require('./sub')
52+
};
53+
54+
The contents of _./math/add.js_ show us a new technique, here we use `module.exports` instead of `exports`. Previously mentioned was the fact that `exports` is not the only object exposed to the module file when evaluated, we also have access to `__dirname`, `__filename`, and `module` which represents the current module. Here we simply define the module export object to a new object, which happens to be a function.
55+
56+
57+
module.exports = function add(a, b){
58+
return a + b;
59+
};
60+
61+
This technique is usually only helpful when your module has one aspect that it wishes to expose, be it a single function, constructor, string, etc. Below is an example of how we could provide the `Animal` constructor:
62+
63+
exports.Animal = function Animal(){};
64+
65+
which can then be utilized as shown:
66+
67+
var Animal = require('./animal').Animal;
68+
69+
if we change our module slightly, we can remove `.Animal`:
70+
71+
module.exports = function Animal(){};
72+
73+
which can now be used without the property:
74+
75+
var Animal = require('./animal');

‎src/modules/app.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ var sys = require('sys'),
55
var a = { one: 1 };
66
var b = { two: 2 };
77
utils.merge(a, b);
8-
sys.p(a);
8+
sys.p(a);
9+
10+
var math = require('./math');
11+
require('sys').puts(require('sys').inspect(math));
12+
sys.puts(math.add(3, 1));
13+
sys.puts(math.sub(3, 1));

‎src/modules/math/add.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module.exports = function add(a, b){
3+
return a + b;
4+
};

‎src/modules/math/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
exports = {
3+
add: require('./add'),
4+
sub: require('./sub')
5+
};

‎src/modules/math/sub.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
module.exports = function sub(a, b){
3+
return a - b;
4+
};

0 commit comments

Comments
 (0)