forked from jsdoc/jsdoc.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
howto-commonjs-modules.html
222 lines (202 loc) · 8.61 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
<!DOCTYPE html>
<!-- THIS IS A GENERATED FILE. DO NOT EDIT. -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Documenting code that conforms to the CommonJS module standard.">
<title>Use JSDoc: Document CommonJS Modules</title>
<link rel="stylesheet" href="styles/usejsdoc.css">
<link rel="stylesheet" href="styles/prettify.css">
<link rel="stylesheet" href="styles/css3-github-ribbon.css">
<script src="scripts/prettify.js"></script>
<!--[if lt IE 9]>
<script src="scripts/html5shiv.min.js"></script>
<script src="scripts/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header>
<a href="./index.html">@use JSDoc</a>
</header>
<article>
<h1>Document CommonJS Modules</h1>
<h2>Table of Contents</h2>
<ul>
<li>
<a href="#overview">Overview</a>
</li>
<li>
<a href="#document-a-simple-commonjs-module">Document a Simple CommonJS Module</a>
</li>
<li>
<a href="#document-members-assigned-to-module-exports-">Document Members Assigned to module.exports</a>
</li>
<li>
<a href="#document-members-exported-on-the-module-s-this-object">Document Members Exported on the Module's this Object</a>
</li>
<li>
<a href="#document-a-function-that-returns-an-amd-module">Document a Function that returns an AMD Module</a>
</li>
<li>
<a href="#document-a-module-as-a-constructor">Document a Module as a Constructor</a>
</li>
<li>
<a href="#document-multiple-amd-modules-defined-in-a-single-file">Document Multiple AMD Modules Defined in a Single File</a>
</li>
</ul>
<h2 id="overview">Overview</h2>
<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>.
In addition, JSDoc supports <a href="http://nodejs.org/api/modules.html">Node.js modules</a>, which extend the CommonJS standard. This allows you to easily
document your own JavaScript modules and the members they export.</p>
<h2 id="document-a-simple-commonjs-module">Document a Simple CommonJS Module</h2>
<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>
<figure>
<figcaption>The putOn and unbutton methods are documented as members of the "my/shirt" module.</figcaption><pre class="prettyprint lang-js"><code>/** @module my/shirt */
/** Try it on. */
exports.putOn = function(someShirt) {
}
/** Make it easier to put on and remove. */
exports.unbutton = function(someShirt) {
}
</code></pre>
</figure>
<h2 id="document-members-assigned-to-module-exports-">Document Members Assigned to <code>module.exports</code></h2>
<p>In a Node.js module, you can assign an object literal to <code>module.exports</code> directly. This pattern is automatically supported by JSDoc 3.</p>
<figure>
<figcaption>The blend and darken methods are documented as members of the "color/mixer" module.</figcaption><pre class="prettyprint lang-js"><code>/** @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>
</figure>
<h2 id="document-members-exported-on-the-module-s-this-object">Document Members Exported on the Module's <code>this</code> Object</h2>
<p>JSDoc 3 understands the convention of exporting properties and functions when they are assigned to the module's <code>this</code> object, as shown below.</p>
<figure>
<figcaption>The Book class is documented as a member of the "bookshelf" module.</figcaption><pre class="prettyprint lang-js"><code>/**
* @module bookshelf
*/
/**
* @class
*/
this.Book = function(title) {
/** The title of the book. */
this.title = title;
}
</code></pre>
</figure>
<h2 id="document-a-function-that-returns-an-amd-module">Document a Function that returns an AMD Module</h2>
<p>Modules can also be written using the <a href="https://github.com/amdjs/amdjs-api/blob/master/AMD.md">Asynchronous Module Definition (AMD) API</a>, which is
implemented by libraries such as <a href="http://requirejs.org/">RequireJS</a>. The AMD format provides a <code>define</code> method that allows you to write
a function to return a module object. Use the <a href="tags-exports.html">@exports</a> tag to document that all the members of an object literal should be
documented as members of a module.</p>
<figure>
<figcaption>The color property and the Turtleneck class are documented as members of the "my/shirt" module.</figcaption><pre class="prettyprint lang-js"><code>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>
</figure>
<h2 id="document-a-module-as-a-constructor">Document a Module as a Constructor</h2>
<p>The following examples illustrate patterns for documenting modules that are constructors.</p>
<figure>
<figcaption>Use the @alias tag to simplify documenting an AMD constructor-module.</figcaption><pre class="prettyprint lang-js"><code>/**
* A module representing a jacket.
* @module jacket
*/
define('jacket', function () {
/**
* @constructor
* @alias module:jacket
*/
var exports = function() {
}
/** Open and close your Jacket. */
exports.prototype.zip = function() {
}
return exports;
});
</code></pre>
</figure>
<p>The same pattern can be documented in CommonJS environments.</p>
<figure>
<figcaption>Use the @alias tag to document constructor-modules in CommonJS.</figcaption><pre class="prettyprint lang-js"><code>/**
* A module representing a jacket.
* @module jacket
*/
/**
* @constructor
* @alias module:jacket
*/
function Jacket() {
}
/** Open and close your Jacket. */
Jacket.prototype.zip = function() {
}
module.exports = Jacket;
</code></pre>
</figure>
<h2 id="document-multiple-amd-modules-defined-in-a-single-file">Document Multiple AMD Modules Defined in a Single File</h2>
<p>If you have multiple calls to <code>define</code> in a single file use the <a href="tags-exports.html">@exports</a> 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>
<figure>
<figcaption>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.</figcaption><pre class="prettyprint lang-js"><code>// 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>
</figure>
</article>
<footer>
<a class="license-badge" href="http://creativecommons.org/licenses/by-sa/3.0/">
<img alt="Creative Commons License" class="license-badge" src="images/cc-by-sa.svg" width="80" height="15" />
</a>
<br> Copyright © 2011-2014 the
<a href="https://github.com/jsdoc3/jsdoc3.github.com/contributors">contributors</a> to the JSDoc 3 documentation project.
<br> This website is <a href="https://github.com/jsdoc3/jsdoc3.github.com">open source</a> and is 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 type="text/javascript">
prettyPrint();
</script>
</body>
</html>