You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ba0b671
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: chapters/modules.md
+31-2Lines changed: 31 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,6 @@ Next we will look at utilizing out new module in other libraries.
34
34
35
35
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()`.
36
36
37
-
38
37
var sys = require('sys'),
39
38
utils = require('./utilities');
40
39
@@ -43,4 +42,34 @@ Create a second file named _./app.js_ with the code shown below. First we load i
43
42
utils.merge(a, b);
44
43
sys.p(a);
45
44
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`:
0 commit comments