-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsDocumentation.js
More file actions
119 lines (107 loc) · 3.67 KB
/
jsDocumentation.js
File metadata and controls
119 lines (107 loc) · 3.67 KB
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
// how to use JSDoc to document a function that takes three arguments and returns a sentence:
/**
*
*/
// - business analysis
/**
* Generates a sentence based on the name, origin, and course.
*
* @param {string} name - The name of the person.
* @param {string} origin - The country of origin of the person.
* @param {string} course - The name of the course.
* @returns {string} - A sentence that includes the name, origin, and course.
*/
function generateSentence(name, origin, course) {
return `${name} from ${origin} is taking the course ${course}.`;
}
/*
Documenting object properties and methods:
In this example, we're using JSDoc to document an object type Person.
We're defining its properties using @property tags, and we're also
documenting a method greet using @method, @memberof, and @instance tags.
This makes it easy for other developers to understand the structure of
the Person object and how to use its methods.
*/
/**
* Represents a person.
*
* @typedef {Object} Person
* @property {string} name - The name of the person.
* @property {number} age - The age of the person.
*
* @method greet
* @memberof Person
* @instance
* @param {string} message - The greeting message.
* @returns {string} - The greeting message.
*/
const person = {
name: "John",
age: 30,
greet(message) {
return `${message}, ${this.name}!`;
},
};
/*
Documenting callbacks
In this example, we're using JSDoc to document a function performAsyncOperation
that takes a callback as a parameter. We're using @param tags to document the
parameters of the callback function, including their data type and what they represent.
This makes it easy for other developers to understand how to use the performAsyncOperation
function and how to handle its callback.
*/
/**
* Performs an asynchronous operation.
*
* @param {function} callback - The callback function to call when the operation is complete.
* @param {Error} callback.error - The error object if the operation failed.
* @param {any} callback.result - The result of the operation if it succeeded.
*/
function performAsyncOperation(callback) {
// Perform the async operation...
const error = null;
const result = "Operation succeeded";
callback(error, result);
}
/*
Documenting complex types:
In this example, we're using JSDoc to document a complex data structure consisting of two
object types Student and Course. We're using @typedef tags to define these types, and we're
also defining the properties of each type using @property tags. This makes it easy for other
developers to understand the structure of the student object and the Course object, and how
they relate to each other.
*/
/**
* Represents a student.
*
* @typedef {Object} Student
* @property {string} name - The name of the student.
* @property {string} email - The email address of the student.
* @property {Course[]} courses - The courses the student is enrolled in.
*/
/**
* Represents a course.
*
* @typedef {Object} Course
* @property {string} name - The name of the course.
* @property {number} credits - The number of credits for the course.
*/
const student = {
name: "Joseph",
email: "joseph@example.com",
courses: [
{
name: "JavaScript Basics",
credits: 3,
},
{
name: "Node.js Fundamentals",
credits: 4,
},
],
};
/*
In conclusion, JSDoc is a powerful tool for documenting your JavaScript code. By using JSDoc
to document your code, you can make it easier for other developers to understand how to use
your code, and to maintain it over time.
*/