|
2 | 2 | <html lang="en">
|
3 | 3 | <head>
|
4 | 4 | <meta charset="utf-8">
|
5 |
| - <meta name="description" content="[todo] Document a JavaScript module."><title>Use JSDoc: @module</title> |
| 5 | + <meta name="description" content="Document a JavaScript module."><title>Use JSDoc: @module</title> |
6 | 6 |
|
7 | 7 | <link rel="stylesheet" href="lib/prettify.css" />
|
8 | 8 | <script src="lib/prettify.js"></script>
|
|
176 | 176 | <article>
|
177 | 177 | <h1>@module</h1>
|
178 | 178 |
|
| 179 | +<h3>Syntax</h3> |
| 180 | +<code>@module [<ModuleName>]</code> |
| 181 | + |
179 | 182 | <h3>Overview</h3>
|
180 | 183 |
|
181 |
| -<p> |
| 184 | +<p>This marks the current file as being its own module. |
| 185 | +All symbols in the file are members of the module (unless documented otherwise, see (e.g.) <a href="tags-memberof.html">@memberof</a>). |
| 186 | +</p> |
| 187 | + |
| 188 | +<p>Link to a module (e.g. within a <a href="tags-link.html">@link</a> or <a href="tags-see.html">@see</a>) using "module:moduleName". For example, "@module foo/bar" can be linked to using "{@link module:foo/bar}". |
182 | 189 | </p>
|
183 | 190 |
|
184 |
| -<h3>Examples</h3> |
| 191 | +<p>If the module name is not provided, it is set to the current filename relative to the folder JSDoc is run on. |
| 192 | +</p> |
185 | 193 |
|
186 |
| -<p> |
| 194 | +<p>For example, suppose I have a file <code>test.js</code> with doclet <code>/** @module */</code> in it, in folder 'src'. |
| 195 | +Here are some scenarios for running JSDoc and the resulting module names for test.js: |
187 | 196 | </p>
|
188 | 197 |
|
189 | 198 | <dl class="example">
|
190 |
| -<dt>Example goes here</dt> |
| 199 | +<dt>Derived module names if none is provided.</dt> |
| 200 | +<dd> |
| 201 | +<pre class="prettyprint lang-js"> |
| 202 | +# from src/ |
| 203 | +jsdoc ./test.js # module name 'test' |
| 204 | + |
| 205 | +# from src's parent directory: |
| 206 | +jsdoc src/test.js # module name 'src/test' |
| 207 | +jsdoc -r src/ # module name 'test' |
| 208 | + |
| 209 | +</pre> |
| 210 | +</dd> |
| 211 | +</dl><h3>Examples</h3> |
| 212 | +<dl class="example"> |
| 213 | +<dt>Basic @module use.</dt> |
191 | 214 | <dd>
|
192 | 215 | <pre class="prettyprint lang-js">
|
193 |
| -// todo |
| 216 | +/** @module myModule */ |
| 217 | + |
| 218 | +/** will be module:myMmodule~foo */ |
| 219 | +var foo = 1; |
194 | 220 |
|
195 | 221 | </pre>
|
196 | 222 | </dd>
|
197 |
| -</dl><h3>See Also</h3> |
| 223 | +</dl>There are many ways to export objects as belonging to a module (i.e. as static members rather than inner as in the example above). These include assigning the objects to <code>module.exports</code> or <code>exports</code> or <code>this</code>. |
| 224 | + |
| 225 | +<dl class="example"> |
| 226 | +<dt>Using 'this' in a file with @module.</dt> |
| 227 | +<dd> |
| 228 | +<pre class="prettyprint lang-js"> |
| 229 | +/** @module bookshelf */ |
| 230 | +/** @class */ |
| 231 | +this.Book = function (title) { |
| 232 | + /** The title. */ |
| 233 | + this.title = title; |
| 234 | +}; |
| 235 | + |
| 236 | +</pre> |
| 237 | +</dd> |
| 238 | +</dl><p>When a global symbol is a member of 'this' in a file with a @module tag, the symbol is documented as a member of that module. |
| 239 | +So the Book class above is documented as <code>module:bookshelf.Book</code>. The title is <code>module:bookshelf.Book#title</code> as expected. |
| 240 | +</p> |
| 241 | + |
| 242 | +<dl class="example"> |
| 243 | +<dt>Documenting under a namespace 'module.exports' or 'exports'.</dt> |
| 244 | +<dd> |
| 245 | +<pre class="prettyprint lang-js"> |
| 246 | +/** @module color/mixer */ |
| 247 | +module.exports = { |
| 248 | + /** Blend two colours together. */ |
| 249 | + blend: function (color1, color2) {} |
| 250 | +}; |
| 251 | +/** Darkens a color. */ |
| 252 | +exports.darken = function (color, shade) {}; |
| 253 | + |
| 254 | +</pre> |
| 255 | +</dd> |
| 256 | +</dl><p>JSDoc recognises assigning to <code>module.exports</code> or <code>exports</code> as assigning to the module. |
| 257 | +Hence the two functions above will have names "module:color/mixer.blend" and "module:color/mixer.darken". |
| 258 | +</p> |
| 259 | + |
| 260 | +See <a href="howto-commonjs-modules.html">Documenting Javascript Modules</a> for further examples of using @module as a constructor, or documenting multiple modules in a file. |
| 261 | + |
| 262 | +<h3>See Also</h3> |
198 | 263 |
|
199 | 264 | <ul>
|
200 |
| - <li><a href="#">...</a></li> |
201 |
| -</ul> |
| 265 | + <li><a href="howto-commonjs-modules.html">Other helpful @module examples</a></li> |
| 266 | + <li><a href="tags-exports.html">@exports</a></li> |
| 267 | + <li><a href="tags-namespace.html">@namespace</a></li> |
| 268 | +</ul> |
| 269 | + |
202 | 270 | </article>
|
203 | 271 |
|
204 | 272 | <footer>
|
|
0 commit comments