-
Notifications
You must be signed in to change notification settings - Fork 1
/
howto-commonjs-modules.html
257 lines (208 loc) · 6.94 KB
/
howto-commonjs-modules.html
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Documenting code that conforms to the CommonJS server-side modules standard."><title>Use JSDoc: Document CommonJS Modules</title>
<link rel="stylesheet" href="lib/prettify.css" />
<script src="lib/prettify.js"></script>
<script type="text/javascript">
/* Make HTML 5 elements stylable in IE */
document.createElement('header');
document.createElement('nav');
document.createElement('article');
document.createElement('footer');
document.createElement('hgroup');
</script>
<style>
body { font: .85em 'Helvetica Neue', Helvetica, Arial, sans-serif; }
body
{
padding: 0;
margin: 0;
}
a, a:visited, a:active { color: #605C89; }
dl
{
margin-left: 0;
padding-left: 0;
}
dt
{
margin-left: 16px;
padding-left: 4px;
margin-top: 8px;
}
dt a, dt a:visited {
color: #3E00B6;
}
dd
{
margin-left: 16px;
padding-left: 4px;
}
code { font: .85em Consolas, "Lucida Console", Monaco, monospace; }
/* Make HTML 5 elements display block-level for consistent styling */
header, nav, article, footer, address { display: block; }
header
{
background-color: #EBEBEB;
color: #006FBB;
margin: 0;
margin-bottom: 32px;
padding: 18px;
font-size: 2.4em;
font-weight: bold;
border-bottom: 1px #C6C6C6 solid;
}
header a, header a:visited {
color: #006FBB;
text-decoration: none;
}
h1 { color: #B93A38; }
article { margin: 18px; }
pre
{
display: block;
border: 1px solid #999;
margin: 12px;
padding: 8px;
}
.example dt { font-weight: bold; }
footer
{
margin: 16px;
margin-top: 32px;
font-style: italic;
font-size: .8em;
}
</style>
</head>
<body>
<header>
<a href="./index.html">@use JSDoc</a>
</header>
<nav>
</nav>
<article>
<h1>Document CommonJS Modules</h1>
<h3>Overview</h3>
<p>
JSDoc 3 has built-in support for JavaScript code that is written to conform to the <a href="http://wiki.commonjs.org/wiki/Modules/1.1">CommonJS Modules standard</a>. This allows you to easily document your own JavaScript modules and the members they export.
</p>
<h3>Document a Simple CommonJS Module</h3>
<p>
Add a single <code>@module <module identifier></code> tag at the top of the file that defines your module and any documented members of the <code>exports</code> object in that file will automatically be included in the documentation for that module.
</p>
<dl class="example">
<dt>The putOn and unbutton methods are documented as members of the "my/shirt" module.</dt>
<dd>
<pre><code class="prettyprint lang-js">/** @module my/shirt */
/** Try it on. */
exports.putOn = function(someShirt) {
}
/** Make it easier to put on and remove. */
exports.unbutton = function(someShirt) {
}</code></pre>
</dd>
</dl><h3>Document Members Assigned to <code>module.exports</code></h3>
<p>
Some implementations of the CommonJS Modules standard allow you to assign an object literal to the <code>module.exports</code> namespace directly. This pattern is automatically supported by JSDoc 3.
</p>
<dl class="example">
<dt>The blend and darken methods are documented as members of the "color/mixer" module.</dt>
<dd>
<pre><code class="prettyprint lang-js">/** @module color/mixer */
module.exports = {
/** Blend two colors together. */
blend: function(color1, color2) { }
}
/** Darken a color by the given shade. */
exports.darken = function(color, shade) { }</code></pre>
</dd>
</dl><h3>Document Members Exported on the Global <code>this</code></h3>
<p>
JSDoc 3 understands the NodeJS convention of exporting properties and functions when they are assigned to a global <code>this</code> variable, as shown below.
</p>
<dl class="example">
<dt>The Book class is documented as a member of the "bookshelf" module.</dt>
<dd>
<pre><code class="prettyprint lang-js">/**
* @module bookshelf
*/
/**
* @class
*/
this.Book = function(title) {
/** The title of the book. */
this.title = title;
}</code></pre>
</dd>
</dl><h3>Document a Function that returns a RequireJS Module</h3>
<p>The RequireJS library provides a <code>define</code> method that allows you to write a function to return a module object. Use the <code>@exports</code> tag to document that all the members of an object literal should be documented as members of a module.</p>
<dl class="example">
<dt>The color property and the Turtleneck class are documented as members of the "my/shirt" module.</dt>
<dd>
<pre><code class="prettyprint lang-js">define('my/shirt', function () {
/**
A module representing a shirt.
@exports my/shirt
@version 1.0
*/
var shirt = {
/** A property of the module. */
color: "black",
/** @constructor */
Turtleneck: function(size) {
/** A property of the class. */
this.size = size;
}
};
return shirt;
});</code></pre>
</dd>
</dl><h3>Document Multiple RequireJS Modules Defined in a Single File</h3>
<p>If you have multiple calls to <code>define</code> in a single file use the <code>@exports</code> tag to document each function that returns module code. Name the exported objects "exports" and JSDoc 3 will automatically document any of their members as members of their module.</p>
<dl class="example">
<dt>The getStyleProperty and isInHead methods are documented as members of the "html/utils" module. The Tag class is documented as a member of the "tag" module.</dt>
<dd>
<pre><code class="prettyprint lang-js">// one module
define('html/utils',
/**
Utility functions to ease working with DOM elements.
@exports html/utils
*/
function() {
var exports = {
/** Get the value of a property on an element. */
getStyleProperty: function(element, propertyName) { }
};
/** Determine if an element is in the document head. */
exports.isInHead = function(element) { }
return exports;
}
);
// another module
define('tag',
/** @exports tag */
function() {
var exports = {
/** @class */
Tag: function(tagName) { }
};
return exports;
}
);</code></pre>
</dd>
</dl><h3>See Also</h3>
<ul>
<li><a href="http://requirejs.org/docs/api.html#define">Defining RequireJS Modules</a></li>
<li><a href="http://nodejs.org/docs/v0.4.1/api/modules.html">NodeJS Modules Tutorial</a></li>
</ul>
</article>
<footer>
Copyright © 2011 Michael Mathews <[email protected]><br>
This site is <a href="https://github.com/micmath/micmath.github.com">open source</a> and licensed under the <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
</footer>
<script>prettyPrint()</script>
</body>
</html>