-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathabout-namepaths.html
173 lines (166 loc) · 6.7 KB
/
about-namepaths.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
<!DOCTYPE html>
<!-- THIS IS A GENERATED FILE. DO NOT EDIT. -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="A guide to using namepaths with JSDoc 3.">
<title>Use JSDoc: Using namepaths with JSDoc 3</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>
</head>
<body>
<header>
<a href="./index.html">@use JSDoc</a>
</header>
<article>
<h1>Using namepaths with JSDoc 3</h1>
<h2>Table of Contents</h2>
<ul>
<li>
<a href="#namepaths-in-jsdoc-3">Namepaths in JSDoc 3</a>
</li>
<li>
<a href="#related-links">Related Links</a>
</li>
</ul>
<h2 id="namepaths-in-jsdoc-3">Namepaths in JSDoc 3</h2>
<p>When referring to a JavaScript variable that is elsewhere in your documentation, you must provide a unique identifier that maps to that variable. A
namepath provides a way to do so and disambiguate between instance members, static members and inner variables.</p>
<figure>
<figcaption>Basic Syntax Examples of Namepaths in JSDoc 3</figcaption>
<pre class="prettyprint"><code>myFunction
MyConstructor
MyConstructor#instanceMember
MyConstructor.staticMember
MyConstructor~innerMember // note that JSDoc 2 uses a dash
</code></pre>
</figure>
<p>The example below shows: an <em>instance</em> method named "say," an <em>inner</em> function also named "say," and a <em>static</em>
method also named "say." These are three distinct methods that all exist independently of one another.</p>
<figure>
<figcaption>Use a documentation tag to describe your code.</figcaption>
<pre class="prettyprint lang-js"><code>/** @constructor */
Person = function() {
this.say = function() {
return "I'm an instance.";
}
function say() {
return "I'm inner.";
}
}
Person.say = function() {
return "I'm static.";
}
var p = new Person();
p.say(); // I'm an instance.
Person.say(); // I'm static.
// there is no way to directly access the inner function from here
</code></pre>
</figure>
<p>You would use three different namepath syntaxes to refer to the three different methods:</p>
<figure>
<figcaption>Use a documentation tag to describe your code.</figcaption>
<pre class="prettyprint"><code>Person#say // the instance method named "say."
Person.say // the static method named "say."
Person~say // the inner method named "say."
</code></pre>
</figure>
<p>You might wonder why there is a syntax to refer to an inner method when that method isn't directly accessible from outside the function it is defined
in. While that is true, and thus the "~" syntax is rarely used, it <em>is</em> possible to return a reference to an inner method from another
method inside that container, so it is possible that some object elsewhere in your code might borrow an inner method.</p>
<p>Note that if a constructor has an instance member that is also a constructor, you can simply chain the namepaths together to form a longer namepath:</p>
<figure>
<figcaption>Use a documentation tag to describe your code.</figcaption>
<pre class="prettyprint lang-js"><code>/** @constructor */
Person = function() {
/** @constructor */
this.Idea = function() {
this.consider = function(){
return "hmmm";
}
}
}
var p = new Person();
var i = new p.Idea();
i.consider();
</code></pre>
</figure>
<p>In this case, to refer to the method named "consider," you would use the following namepath:
<code>Person#Idea#consider</code></p>
<p>This chaining can be used with any combination of the connecting symbols: <code># . ~</code></p>
<figure>
<figcaption>Special cases: modules, externals and events.</figcaption>
<pre class="prettyprint lang-js"><code>/** A module. Its name is module:foo/bar.
* @module foo/bar
*/
/** The built in string object. Its name is external:String.
* @external String
*/
/** An event. Its name is module:foo/bar.event:MyEvent.
* @event module:foo/bar.event:MyEvent
*/
</code></pre>
</figure>
<p>There are some special cases with namepaths: <a href="tags-module.html">@module</a> names are prefixed by "module:", <a
href="tags-external.html">@external</a> names are prefixed by "external:", and <a href="tags-event.html">@event</a> names are prefixed by
"event:".</p>
<figure>
<figcaption>Namepaths of objects with special characters in the name.</figcaption>
<pre class="prettyprint lang-js"><code>/** @namespace */
var chat = {
/**
* Refer to this by {@link chat."#channel"}.
* @namespace
*/
"#channel": {
/**
* Refer to this by {@link chat."#channel".open}.
* @type {boolean}
* @defaultvalue
*/
open: true,
/**
* Internal quotes have to be escaped by backslash. This is
* {@link chat."#channel"."say-\"hello\""}.
*/
'say-"hello"': function (msg) {}
}
};
/**
* Now we define an event in our {@link chat."#channel"} namespace.
* @event chat."#channel"."op:announce-motd"
*/
</code></pre>
</figure>
<p>Above is an example of a namespace with "unusual" characters in its member names (the hash character, dashes, even quotes).
To refer to these you just need quote the names: chat."#channel", chat."#channel"."op:announce-motd", and so on.
Internal quotes in names should be escaped with backslashes: chat."#channel"."say-"hello"".</p>
<h2 id="related-links">Related Links</h2>
<ul>
<li>
<a href="about-block-inline-tags.html">Block and inline tags</a>
</li>
<li>
<a href="tags-inline-link.html">{@link}</a>
</li>
</ul>
</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-2019 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>