-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
54 lines (40 loc) · 1.19 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
An implementation of the CommonJS Module Specification
http://wiki.commonjs.org/wiki/Modules/1.1
... with the following changes to facilitate implementation in the browser:
- No script loading is implemented; you must load all modules yourself in
whatever order might be required.
- You must bind the "exports" variable yourself at the top of every module
by assigning it the return value of module.exports("<yourModuleName>")
Since in the browser modules use the global namespace instead of their own, I
recommend creating a scope for each module (see examples).
Examples:
math.js
-------
(function (exports)
{
exports.add = function() {
var sum = arguments[0];
for (var i=1; i<arguments.length; i++) {
sum += arguments[i];
}
return sum;
};
}(module.exports('math'));
increment.js
------------
(function (exports)
{
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
}(module.exports('increment'));
program.js
----------
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
Less noteworthy departures from the spec:
- "require" does not have a "main" property.
- Dependency cycles not allowed.
- "module.id" is writeable/deleteable.