Skip to content

Commit e7734f4

Browse files
committed
Document this/extends/class/enum
1 parent a1dabea commit e7734f4

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

JSDoc-support-in-JavaScript.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Note any tags which are not explicitly listed below (such as `@async`) are not y
1111
* `@template`
1212
* `@class` (or `@constructor`)
1313
* `@this`
14-
* `@augments` (or `@extends`)
14+
* `@extends` (or `@augments`)
1515
* `@enum`
1616

1717
The meaning is usually the same, or a superset, of the meaning of the tag given at usejsdoc.org.
@@ -315,6 +315,89 @@ function seriousalize(key, object) {
315315
}
316316
```
317317

318+
## `@constructor`
319+
320+
The compiler infers constructor functions based on this-property assignments, but you can make checking stricter and suggestions better if you add a `@constructor` tag:
321+
322+
```js
323+
/**
324+
* @constructor
325+
* @param {number} data
326+
*/
327+
function C(data) {
328+
this.size = 0;
329+
this.initialize(data); // Should error, initializer expects a string
330+
}
331+
/**
332+
* @param {string} s
333+
*/
334+
C.prototype.initialize = function (s) {
335+
this.size = s.length
336+
}
337+
338+
var c = new C(0);
339+
var result = C(1); // C should only be called with new
340+
```
341+
342+
With `@constructor`, `this` is checked inside the constructor function `C`, so you will get suggestions for the `initialize` method and an error if you pass it a number. You will also get an error if you call `C` instead of constructing it.
343+
344+
Unfortunately, this means that constructor functions that are also callable cannot use `@constructor`.
345+
346+
## `@this`
347+
348+
The compiler can usually figure out the type of `this` when it has some context to work with. When it doesn't, you can explicitly specify the type of `this` with `@this`:
349+
350+
```js
351+
/**
352+
* @this {HTMLElement}
353+
* @param {*} e
354+
*/
355+
function callbackForLater(e) {
356+
this.clientHeight = parseInt(e) // should be fine!
357+
}
358+
```
359+
360+
## `@extends`
361+
362+
When Javascript classes extend a generic base class, there is nowhere to specify what the type parameter should be. The `@extends` tag provides a place for that type parameter:
363+
364+
```js
365+
/**
366+
* @template T
367+
* @extends {Set<T>}
368+
*/
369+
class SortableSet extends Set {
370+
// ...
371+
}
372+
```
373+
374+
Note that `@extends` only works with classes. Currently, there is no way for a constructor function extend a class.
375+
376+
377+
## `@enum`
378+
379+
The `@enum` tag allows you to create an object literal whose members are all of a specified type. Unlike most object literals in Javascript, it does not allow other members.
380+
381+
```js
382+
/** @enum {number} */
383+
const JSDocState = {
384+
BeginningOfLine: 0,
385+
SawAsterisk: 1,
386+
SavingComments: 2,
387+
}
388+
```
389+
390+
Note that `@enum` is quite different from, and much simpler than, Typescript's `enum`. However, unlike Typescript's enums, `@enum` can have any type:
391+
392+
```js
393+
/** @enum {function(number): number} */
394+
const Math = {
395+
add1: n => n + 1,
396+
id: n => -n,
397+
sub1: n => n - 1,
398+
}
399+
```
400+
318401
## More examples
319402

320403
```js

0 commit comments

Comments
 (0)