Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 272632b

Browse files
committedAug 4, 2013
improve docs for param tag
1 parent 9aebf20 commit 272632b

File tree

2 files changed

+197
-59
lines changed

2 files changed

+197
-59
lines changed
 

‎Jake/articles/tags-param

Lines changed: 92 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,35 @@
1414
<h3>Overview</h3>
1515

1616
<p>
17-
The @param tag (or @arg or @argument) documents a parameter of a function.
17+
The @param tag (or @arg or @argument) documents a function parameter.
18+
</p>
19+
20+
<p>
21+
The @param tag requires you to specify the name of the parameter you are documenting. You can also
22+
include the parameter's type, enclosed in curly brackets, and a description of the parameter.
23+
</p>
24+
25+
<p>
26+
The parameter type can be a built-in JavaScript type, such as <code>string</code> or
27+
<code>Object</code>, or a <a href="about-namepaths.html">JSDoc namepath</a> to another symbol in
28+
your code. If you have written documentation for the symbol at that namepath, JSDoc will
29+
automatically link to the documentation for that symbol. You can also use a type expression to
30+
indicate, for example, that a parameter is not nullable or can accept any type; see the
31+
<a href="tags-type.html">@type documentation</a> for details.
32+
</p>
33+
34+
<p>
35+
If you provide a description, you can make the JSDoc comment more readable by inserting a hyphen
36+
before the description. Be sure to include a space before and after the hyphen.
1837
</p>
1938

2039
<h3>Examples</h3>
2140

2241
<p>
23-
The following examples show the basic usage of @param.
42+
The following examples show how to include names, types, and descriptions in a @param tag.
2443
</p>
2544

26-
{{#example}}Basic usage of @param
45+
{{#example}}Name only
2746
/**
2847
* @param somebody
2948
*/
@@ -32,7 +51,7 @@ function sayHello(somebody) {
3251
}
3352
{{/example}}
3453

35-
{{#example}}Using a type with @param
54+
{{#example}}Name and type
3655
/**
3756
* @param {string} somebody
3857
*/
@@ -42,22 +61,36 @@ function sayHello(somebody) {
4261
{{/example}}
4362

4463

45-
{{#example}}Using type and description with @param
64+
{{#example}}Name, type, and description
4665
/**
47-
* @param {string} somebody Name
66+
* @param {string} somebody Somebody's name.
4867
*/
4968
function sayHello(somebody) {
5069
alert('Hello ' + somebody);
5170
}
5271
{{/example}}
5372

5473
<p>
55-
The following examples show how to indicate that a parameter can be optional and has a default value
74+
You can add a hyphen before the description to make it more readable. Be sure to include a space
75+
before and after the hyphen.
5676
</p>
5777

58-
{{#example}}An optional parameter
78+
{{#example}}Name, type, and description, with a hyphen before the description
5979
/**
60-
* @param {string} [somebody] Name
80+
* @param {string} somebody - Somebody's name.
81+
*/
82+
function sayHello(somebody) {
83+
alert('Hello ' + somebody);
84+
}
85+
{{/example}}
86+
87+
<p>
88+
The following examples show how to indicate that a parameter is optional and has a default value.
89+
</p>
90+
91+
{{#example}}An optional parameter (using JSDoc syntax)
92+
/**
93+
* @param {string} [somebody] - Somebody's name.
6194
*/
6295
function sayHello(somebody) {
6396
if (!somebody) {
@@ -67,9 +100,22 @@ function sayHello(somebody) {
67100
}
68101
{{/example}}
69102

70-
{{#example}}Optional parameter and default value
103+
{{#example}}An optional parameter (using Google Closure Compiler syntax)
71104
/**
72-
* @param {String} [somebody=John Doe] Name
105+
* @param {string=} somebody - Somebody's name.
106+
*/
107+
function sayHello(somebody) {
108+
if (!somebody) {
109+
somebody = 'John Doe';
110+
}
111+
alert('Hello ' + somebody);
112+
}
113+
114+
{{/example}}
115+
116+
{{#example}}An optional parameter and default value
117+
/**
118+
* @param {string} [somebody=John Doe] - Somebody's name.
73119
*/
74120
function sayHello(somebody) {
75121
if (!somebody) {
@@ -80,13 +126,15 @@ function sayHello(somebody) {
80126
{{/example}}
81127

82128
<p>
83-
A parameter could also have several types instead of just one. It can also be used multiple times.
84-
The following examples reflect these situations.
129+
The following examples show how to use type expressions to indicate that a parameter can accept
130+
multiple types (or any type), and that a parameter can be provided more than once. See the
131+
<a href="tags-type.html">@type documentation</a> for details about the type expressions that JSDoc
132+
supports.
85133
</p>
86134

87-
{{#example}}Multiple types
135+
{{#example}}Allows one type OR another type (type union)
88136
/**
89-
* @param {String|Array} [somebody=John Doe] Name or array of names
137+
* @param {(string|string[])} [somebody=John Doe] - Somebody's name, or an array of names.
90138
*/
91139
function sayHello(somebody) {
92140
if (!somebody) {
@@ -98,42 +146,57 @@ function sayHello(somebody) {
98146
}
99147
{{/example}}
100148

101-
{{#example}}Variable parameter
149+
{{#example}}Allows any type
150+
/**
151+
* @param {*} somebody - Whatever you want.
152+
*/
153+
function sayHello(somebody) {
154+
console.log('Hello ' + JSON.stringify(somebody));
155+
}
156+
{{/example}}
157+
158+
{{#example}}Allows a parameter to be repeated
102159
/**
103160
* Returns the sum of all numbers passed to the function.
104-
* @param {...Number} num A positive or negative number
161+
* @param {...number} num - A positive or negative number.
105162
*/
106163
function sum(num) {
107-
var i=0, n=arguments.length, t=0;
108-
for (; i&lt;n; i++) {
164+
var i = 0, n = arguments.length, t = 0;
165+
for (; i &lt; n; i++) {
109166
t += arguments[i];
110167
}
111168
return t;
112169
}
113170
{{/example}}
114171

115-
{{#example}}Parameters that are callbacks
116-
/**
117-
* Does something asynchronously, calling the callback on completion.
118-
* @param {requestCallback} cb - The callback that handles the response.
119-
*/
120-
function doSomethingAsynchronously(cb) {
121-
// code
122-
};
172+
<p>
173+
If a parameter accepts a callback function, you can use the <a href="tags-callback.html">@callback
174+
tag</a> to define a callback type, then include the callback type in the @param tag.
175+
</p>
123176

177+
{{#example}}Parameters that accept a callback
124178
/**
125-
* This callback is displayed as a global member.
179+
* This callback type is called `requestCallback` and is displayed as a global symbol.
180+
*
126181
* @callback requestCallback
127182
* @param {number} responseCode
128183
* @param {string} responseMessage
129184
*/
185+
186+
/**
187+
* Does something asynchronously and executes the callback on completion.
188+
* @param {requestCallback} cb - The callback that handles the response.
189+
*/
190+
function doSomethingAsynchronously(cb) {
191+
// code
192+
};
130193
{{/example}}
131194

132195
<h3>See Also</h3>
133196

134197
<ul>
198+
<li><a href="tags-callback.html">@callback</a></li>
135199
<li><a href="tags-returns.html">@returns</a></li>
136200
<li><a href="tags-type.html">@type</a></li>
137201
<li><a href="tags-typedef.html">@typedef</a></li>
138-
<li><a href="tags-callback.html">@callback</a></li>
139202
</ul>

‎tags-param.html

Lines changed: 105 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,36 @@ <h3>Synonyms</h3>
187187
<h3>Overview</h3>
188188

189189
<p>
190-
The @param tag (or @arg or @argument) documents a parameter of a function.
190+
The @param tag (or @arg or @argument) documents a function parameter.
191+
</p>
192+
193+
<p>
194+
The @param tag requires you to specify the name of the parameter you are documenting. You can also
195+
include the parameter's type, enclosed in curly brackets, and a description of the parameter.
196+
</p>
197+
198+
<p>
199+
The parameter type can be a built-in JavaScript type, such as <code>string</code> or
200+
<code>Object</code>, or a <a href="about-namepaths.html">JSDoc namepath</a> to another symbol in
201+
your code. If you have written documentation for the symbol at that namepath, JSDoc will
202+
automatically link to the documentation for that symbol. You can also use a type expression to
203+
indicate, for example, that a parameter is not nullable or can accept any type; see the
204+
<a href="tags-type.html">@type documentation</a> for details.
205+
</p>
206+
207+
<p>
208+
If you provide a description, you can make the JSDoc comment more readable by inserting a hyphen
209+
before the description. Be sure to include a space before and after the hyphen.
191210
</p>
192211

193212
<h3>Examples</h3>
194213

195214
<p>
196-
The following examples show the basic usage of @param.
215+
The following examples show how to include names, types, and descriptions in a @param tag.
197216
</p>
198217

199218
<dl class="example">
200-
<dt>Basic usage of @param</dt>
219+
<dt>Name only</dt>
201220
<dd>
202221
<pre class="prettyprint lang-js">
203222
/**
@@ -210,7 +229,7 @@ <h3>Examples</h3>
210229
</pre>
211230
</dd>
212231
</dl><dl class="example">
213-
<dt>Using a type with @param</dt>
232+
<dt>Name and type</dt>
214233
<dd>
215234
<pre class="prettyprint lang-js">
216235
/**
@@ -223,11 +242,29 @@ <h3>Examples</h3>
223242
</pre>
224243
</dd>
225244
</dl><dl class="example">
226-
<dt>Using type and description with @param</dt>
245+
<dt>Name, type, and description</dt>
246+
<dd>
247+
<pre class="prettyprint lang-js">
248+
/**
249+
* @param {string} somebody Somebody's name.
250+
*/
251+
function sayHello(somebody) {
252+
alert('Hello ' + somebody);
253+
}
254+
255+
</pre>
256+
</dd>
257+
</dl><p>
258+
You can add a hyphen before the description to make it more readable. Be sure to include a space
259+
before and after the hyphen.
260+
</p>
261+
262+
<dl class="example">
263+
<dt>Name, type, and description, with a hyphen before the description</dt>
227264
<dd>
228265
<pre class="prettyprint lang-js">
229266
/**
230-
* @param {string} somebody Name
267+
* @param {string} somebody - Somebody's name.
231268
*/
232269
function sayHello(somebody) {
233270
alert('Hello ' + somebody);
@@ -236,15 +273,15 @@ <h3>Examples</h3>
236273
</pre>
237274
</dd>
238275
</dl><p>
239-
The following examples show how to indicate that a parameter can be optional and has a default value
276+
The following examples show how to indicate that a parameter is optional and has a default value.
240277
</p>
241278

242279
<dl class="example">
243-
<dt>An optional parameter</dt>
280+
<dt>An optional parameter (using JSDoc syntax)</dt>
244281
<dd>
245282
<pre class="prettyprint lang-js">
246283
/**
247-
* @param {string} [somebody] Name
284+
* @param {string} [somebody] - Somebody's name.
248285
*/
249286
function sayHello(somebody) {
250287
if (!somebody) {
@@ -256,11 +293,28 @@ <h3>Examples</h3>
256293
</pre>
257294
</dd>
258295
</dl><dl class="example">
259-
<dt>Optional parameter and default value</dt>
296+
<dt>An optional parameter (using Google Closure Compiler syntax)</dt>
260297
<dd>
261298
<pre class="prettyprint lang-js">
262299
/**
263-
* @param {String} [somebody=John Doe] Name
300+
* @param {string=} somebody - Somebody's name.
301+
*/
302+
function sayHello(somebody) {
303+
if (!somebody) {
304+
somebody = 'John Doe';
305+
}
306+
alert('Hello ' + somebody);
307+
}
308+
309+
310+
</pre>
311+
</dd>
312+
</dl><dl class="example">
313+
<dt>An optional parameter and default value</dt>
314+
<dd>
315+
<pre class="prettyprint lang-js">
316+
/**
317+
* @param {string} [somebody=John Doe] - Somebody's name.
264318
*/
265319
function sayHello(somebody) {
266320
if (!somebody) {
@@ -272,16 +326,18 @@ <h3>Examples</h3>
272326
</pre>
273327
</dd>
274328
</dl><p>
275-
A parameter could also have several types instead of just one. It can also be used multiple times.
276-
The following examples reflect these situations.
329+
The following examples show how to use type expressions to indicate that a parameter can accept
330+
multiple types (or any type), and that a parameter can be provided more than once. See the
331+
<a href="tags-type.html">@type documentation</a> for details about the type expressions that JSDoc
332+
supports.
277333
</p>
278334

279335
<dl class="example">
280-
<dt>Multiple types</dt>
336+
<dt>Allows one type OR another type (type union)</dt>
281337
<dd>
282338
<pre class="prettyprint lang-js">
283339
/**
284-
* @param {String|Array} [somebody=John Doe] Name or array of names
340+
* @param {(string|string[])} [somebody=John Doe] - Somebody's name, or an array of names.
285341
*/
286342
function sayHello(somebody) {
287343
if (!somebody) {
@@ -295,51 +351,70 @@ <h3>Examples</h3>
295351
</pre>
296352
</dd>
297353
</dl><dl class="example">
298-
<dt>Variable parameter</dt>
354+
<dt>Allows any type</dt>
355+
<dd>
356+
<pre class="prettyprint lang-js">
357+
/**
358+
* @param {*} somebody - Whatever you want.
359+
*/
360+
function sayHello(somebody) {
361+
console.log('Hello ' + JSON.stringify(somebody));
362+
}
363+
364+
</pre>
365+
</dd>
366+
</dl><dl class="example">
367+
<dt>Allows a parameter to be repeated</dt>
299368
<dd>
300369
<pre class="prettyprint lang-js">
301370
/**
302371
* Returns the sum of all numbers passed to the function.
303-
* @param {...Number} num A positive or negative number
372+
* @param {...number} num - A positive or negative number.
304373
*/
305374
function sum(num) {
306-
var i=0, n=arguments.length, t=0;
307-
for (; i&lt;n; i++) {
375+
var i = 0, n = arguments.length, t = 0;
376+
for (; i &lt; n; i++) {
308377
t += arguments[i];
309378
}
310379
return t;
311380
}
312381

313382
</pre>
314383
</dd>
315-
</dl><dl class="example">
316-
<dt>Parameters that are callbacks</dt>
384+
</dl><p>
385+
If a parameter accepts a callback function, you can use the <a href="tags-callback.html">@callback
386+
tag</a> to define a callback type, then include the callback type in the @param tag.
387+
</p>
388+
389+
<dl class="example">
390+
<dt>Parameters that accept a callback</dt>
317391
<dd>
318392
<pre class="prettyprint lang-js">
319393
/**
320-
* Does something asynchronously, calling the callback on completion.
394+
* This callback type is called `requestCallback` and is displayed as a global symbol.
395+
*
396+
* @callback requestCallback
397+
* @param {number} responseCode
398+
* @param {string} responseMessage
399+
*/
400+
401+
/**
402+
* Does something asynchronously and executes the callback on completion.
321403
* @param {requestCallback} cb - The callback that handles the response.
322404
*/
323405
function doSomethingAsynchronously(cb) {
324406
// code
325407
};
326408

327-
/**
328-
* This callback is displayed as a global member.
329-
* @callback requestCallback
330-
* @param {number} responseCode
331-
* @param {string} responseMessage
332-
*/
333-
334409
</pre>
335410
</dd>
336411
</dl><h3>See Also</h3>
337412

338413
<ul>
414+
<li><a href="tags-callback.html">@callback</a></li>
339415
<li><a href="tags-returns.html">@returns</a></li>
340416
<li><a href="tags-type.html">@type</a></li>
341417
<li><a href="tags-typedef.html">@typedef</a></li>
342-
<li><a href="tags-callback.html">@callback</a></li>
343418
</ul>
344419

345420
</article>

0 commit comments

Comments
 (0)
Please sign in to comment.