You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: JSDoc-support-in-JavaScript.md
+84-1Lines changed: 84 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Note any tags which are not explicitly listed below (such as `@async`) are not y
11
11
*`@template`
12
12
*`@class` (or `@constructor`)
13
13
*`@this`
14
-
*`@augments` (or `@extends`)
14
+
*`@extends` (or `@augments`)
15
15
*`@enum`
16
16
17
17
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) {
315
315
}
316
316
```
317
317
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
+
functionC(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 =newC(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
+
functioncallbackForLater(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
+
* @templateT
367
+
* @extends{Set<T>}
368
+
*/
369
+
classSortableSetextendsSet {
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
+
constJSDocState= {
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:
0 commit comments