Documenting Your JavaScript | JSDoc Crash Course

Traversy Media
18 Nov 201936:31

Summary

TLDRThis video tutorial explores JSDoc, a JavaScript documentation generator that simplifies creating a documentation website for code. It demonstrates how to install JSDoc, configure it, and utilize inline comments for variables, functions, and classes to generate documentation with type checking. The host also covers custom types, linking documentation, and tutorials integration, showcasing the tool's ability to enhance code clarity and collaboration.

Takeaways

  • 😀 The video introduces JSDoc, a JavaScript documentation generator that creates a documentation website from inline JSDoc comments in the source code.
  • 🛠️ JSDoc is versatile, suitable for both beginners and power users, and can be used with various frameworks like Node, Express, React, or Vue.
  • 📝 It allows for the documentation of variables, classes, functions, modules, and more, including their types and descriptions.
  • 🔍 JSDoc supports type checking similar to TypeScript, but without the need for compilation, and can be used alongside TypeScript.
  • 📚 The documentation website created by JSDoc includes detailed information about function parameters, return types, and source code locations.
  • 🔧 A configuration file (`jsdoc.json`) is used to customize JSDoc's behavior, such as specifying source files and excluding directories.
  • 📖 The script demonstrates how to install JSDoc using npm as a dev dependency and set up a basic configuration file for documentation generation.
  • 🎨 Customization of the documentation template is possible, either by editing the default template or creating a custom template without modifying the `node_modules` folder.
  • 🔗 JSDoc supports creating links within the documentation to other parts of the code, aiding in navigating complex projects.
  • 📝 The script covers how to document functions and parameters, create custom types with typedef, and document classes and their methods.
  • 📑 The video also explains how to include tutorials, readme files, and other additional documentation to enhance the understanding of the codebase.

Q & A

  • What is the purpose of JSDoc mentioned in the video?

    -JSDoc is a JavaScript documentation generator that allows developers to add inline JSDoc comments to their source code, which can then be used to create a comprehensive documentation website for the code.

  • How does JSDoc help with type checking?

    -JSDoc can be used for type checking by adding type annotations in the comments. This helps in identifying type mismatches during development, similar to TypeScript, without the need for compilation.

  • What are the benefits of using JSDoc over TypeScript?

    -JSDoc provides the benefits of type checking and documentation generation without the need for compiling the code, as is required with TypeScript.

  • Can JSDoc be used with TypeScript?

    -Yes, JSDoc can be used with TypeScript, although the video does not go into detail about how to integrate the two.

  • What is the recommended way to install JSDoc in a project?

    -JSDoc can be installed as a dev dependency using npm with the command 'npm install jsdoc --save-dev'.

  • Why is a configuration file needed for JSDoc?

    -A configuration file tells JSDoc where to look for source code, which files to include or exclude, and other settings like the use of plugins and whether to recurse into subfolders.

  • How can JSDoc be integrated with Visual Studio Code?

    -The video does not explicitly mention integration with Visual Studio Code, but typically, one would use the installed JSDoc package and configure it through a 'jsdoc.json' file to work with the editor.

  • What are some of the tags that can be used in JSDoc comments?

    -Some of the tags used in JSDoc comments include @param for function parameters, @returns or @return for the return value of a function, and custom tags for additional documentation like tutorials or examples.

  • How can JSDoc comments be customized for a project?

    -JSDoc comments can be customized by using specific tags that describe the code, such as @param for parameters, @returns for return values, and by adding descriptions and type information.

  • What is the process for generating documentation with JSDoc?

    -To generate documentation with JSDoc, one must first write JSDoc comments in the source code, create a 'jsdoc.json' configuration file, set up a script in 'package.json' to run JSDoc, and then execute the script using npm run jsdoc.

  • How can JSDoc be used to document a class in JavaScript?

    -JSDoc can document a class by using the appropriate JSDoc comments above the class declaration. This includes a class description, constructor parameters, and properties and methods with their respective types and descriptions.

  • Can JSDoc generate documentation for modules?

    -Yes, JSDoc can generate documentation for both CommonJS and ES2015 modules by using the @module tag and documenting the functions or variables exported from the module.

  • What additional features can be added to JSDoc documentation using tutorials?

    -Tutorials can be added to JSDoc documentation to provide more in-depth explanations or guides on how to use the documented code. These can be written in HTML, Markdown, or other supported formats.

  • How can the appearance of the generated documentation be customized?

    -The appearance of the generated documentation can be customized by editing the template files. It's recommended to copy the default template from the 'node_modules' folder to the project directory and make changes there instead of editing the original files directly.

  • What are some of the benefits of using JSDoc for a project?

    -JSDoc provides benefits such as improved code readability, easier maintenance, better collaboration among developers, and the ability to perform type checking during development.

Outlines

00:00

📚 Introduction to JSDoc

The video introduces JSDoc, a JavaScript documentation generator that allows developers to embed documentation comments within their source code. It highlights the tool's capabilities, such as creating a documentation website, supporting type checking, and being compatible with various JavaScript environments including Node.js, React, and Vue. The tutorial also briefly mentions the documentation's potential for enhancing code readability and collaboration.

05:04

🛠 Setting Up JSDoc

This section walks through the initial setup of JSDoc, including installing it as a dev dependency using npm and creating a configuration file. The importance of the source folder, file inclusion and exclusion, and the use of plugins for markdown support and linking are discussed. It also demonstrates how to create a script in the package.json file to run JSDoc and generate documentation.

10:05

🔍 Exploring JSDoc Features

The video explores various features of JSDoc, such as documenting variables, types, function parameters, and descriptions. It shows how to use inline JSDoc comments to annotate code and generate documentation dynamically. The tutorial also touches on the ability to perform type checking similar to TypeScript without compilation and how to customize documentation templates.

15:08

🎨 Customizing Documentation Templates

The presenter explains how to customize JSDoc templates by copying the default template and modifying it to create a unique look for the documentation website. It emphasizes the importance of not editing the original template files within the node_modules folder and demonstrates how to point JSDoc to the custom template using the configuration file.

20:10

🔢 Documenting Data Types and Objects

This part of the video focuses on documenting different data types such as strings, numbers, booleans, and objects using JSDoc. It shows how to define custom types with type definitions and how to enforce type checking within the code editor for better code quality and maintenance.

25:11

📘 Documenting Functions and Parameters

The tutorial moves on to document functions and their parameters using JSDoc. It illustrates how to describe function behavior, parameter details, and return types. The video also explains how JSDoc can automatically generate a table of parameters and their descriptions in the documentation, aiding in code comprehension and collaboration.

30:13

🏫 Creating Tutorials and Links

The video introduces the concept of adding tutorials to the JSDoc documentation. It shows how to create tutorial files in HTML or markdown format and how to link these tutorials to specific modules or code sections. The presenter also demonstrates adding external links and customizing the home page of the documentation.

35:17

📝 Adding a README and Final Touches

In the final part of the video, the presenter discusses adding a README file to the JSDoc documentation for an overview of the project. It also suggests customizing the home page with additional information such as author details, external links, and file descriptions. The video concludes by assigning homework to the viewers, encouraging them to implement JSDoc in their own projects.

Mindmap

Keywords

💡JSDoc

JSDoc is a JavaScript documentation generator that allows developers to annotate their source code with special comments. These comments, referred to as JSDoc comments, are then used to create an entire documentation website for the code. In the video, JSDoc is central to the theme as it is demonstrated how to integrate it into a project to document variables, classes, functions, and modules, enhancing code readability and maintainability.

💡Type Checking

Type checking in the context of the video refers to the process of verifying the data types of variables against their declared types in JSDoc comments. This feature is akin to TypeScript's type system but operates on JavaScript code. The script mentions using JSDoc for type checking to ensure variables are used with their intended types, providing an example where assigning a number to a string type variable results in a type error.

💡Documentation Website

A documentation website, as discussed in the video, is a web-based platform automatically generated by JSDoc from annotated source code. It provides a detailed overview of the project's codebase, including variables, functions, classes, and their usage. The script illustrates the process of generating such a website, which serves as a comprehensive reference for developers.

💡Inline Comments

Inline comments are annotations placed directly in the source code to explain what the code does. In the video, the term is used in the context of JSDoc, where developers add special comments in the form of '/** ... */' within the code to document functions, variables, and their usage. These comments are then parsed by JSDoc to create the documentation.

💡Templates

Templates in the JSDoc context refer to the predefined HTML structures that format the generated documentation. The video mentions the ability to customize these templates or use default ones to style the documentation website. It also shows how to point JSDoc to a custom template for a personalized documentation look.

💡Type Definitions

Type definitions in JSDoc are custom types created by the developer to specify the structure of an object. In the script, a 'student' type is defined with properties such as 'ID', 'name', 'age', and 'isActive'. This allows for more precise type checking and documentation of complex data structures used within the code.

💡Modules

Modules in JavaScript are a means of organizing code into reusable, interchangeable pieces. The video discusses how JSDoc can document modules, whether they are using CommonJS 'require' syntax or ES2015 'import/export' syntax. It demonstrates documenting a 'calculator' module with functions for basic arithmetic operations.

💡Tutorials

The script touches on the ability of JSDoc to include tutorials in the generated documentation. Tutorials can be written in HTML or markdown and are used to provide additional educational content or guides alongside the code documentation. An example is given where 'program-tutorial.html' and 'calculator-tutorial.md' are created to supplement the documentation.

💡Readme

A readme file is a text file that provides information about the project, its purpose, and how to use it. In the video, the concept of including a readme file in the JSDoc configuration is introduced, allowing the readme content to be part of the generated documentation. This is useful for giving an overview and instructions to users or developers interacting with the code.

💡Annotations

Annotations in the video refer to the JSDoc comments that are inserted into the source code to provide metadata about the code's functionality. These annotations are key to the documentation process as they describe the types, parameters, return values, and overall purpose of functions, variables, and classes.

Highlights

Introduction to JSDoc, a JavaScript documentation generator that creates a documentation website from inline comments in the source code.

JSDoc's capability to document variables, classes, functions, modules, and their types, enhancing code readability and maintainability.

The use of JSDoc for type checking similar to TypeScript without the need for compilation.

Compatibility of JSDoc with both backend (Node/Express) and frontend technologies like React or Vue.

Installation of JSDoc via npm and setup as a dev dependency in a project.

Explanation of the JSDoc configuration file and its role in defining source files and documentation settings.

How to create a documentation website with JSDoc by running a script from the package.json file.

Demonstration of adding JSDoc comments for variables, including types and descriptions, and generating documentation.

JSDoc's type checking feature, which provides real-time feedback on type errors in the code editor.

Enabling type checking in JSDoc by adjusting settings in the Visual Studio Code settings.json file.

Creating custom types using typedef in JSDoc for complex data structures.

Linking JSDoc comments to specific parts of the documentation for better navigation.

Documenting functions and their parameters in JSDoc, including return types and descriptions.

How to create and use a class in JSDoc, including constructors and methods.

Inclusion of tutorials in JSDoc documentation to provide additional learning resources.

Customization of the JSDoc template to fit the project's needs without altering the original files.

JSDoc's support for both CommonJS and ES2015 modules, documenting functions and their usage.

Assignment of homework to the viewers to implement JSDoc in an existing JavaScript project as a practical exercise.

Encouragement for viewers to explore JSDoc further and apply it to their projects for better code documentation.

Transcripts

play00:00

[Music]

play00:06

this video is sponsored by lenôtre what

play00:09

technology stack you build on the node

play00:11

makes it easy for both beginners and

play00:12

power users to host apps sites and

play00:15

projects in the cloud to get $20 towards

play00:17

your new account visit Llano comm /

play00:19

travis e hey what's going on guys in

play00:22

this video we're gonna look at Jas dock

play00:24

which is something I've been

play00:25

experimenting with lately that I really

play00:27

like it is a JavaScript documentation

play00:30

generator so basically you can add these

play00:33

inline j/s doc comments right into your

play00:36

source code and you can use Jas doc to

play00:39

scan through and create an entire

play00:41

documentation website for your code and

play00:44

you document just variables classes

play00:47

functions modules whatever function

play00:50

parameters their types you can add

play00:53

descriptions you can even add tutorials

play00:55

and stuff like that so it's really cool

play00:57

and you can also use it for type

play00:59

checking so I'm going to show you how we

play01:00

can do that similar to how you would use

play01:02

typescript only you don't need to

play01:04

compile it like typescript and you can

play01:06

also use it with typescript which I'm

play01:08

not going to get into but you can do

play01:10

that as well and this is the

play01:12

documentation j/s dot app and there's a

play01:15

lot of good information here you can see

play01:17

all the different tags I'm not going to

play01:18

go over all of these because this is a

play01:20

crash course but you can look more into

play01:22

it on this website here so let's jump

play01:26

into vs code and we can install J a

play01:28

stock with NPM and you can use it on the

play01:31

backend with node Express you can also

play01:34

use it on the front end you can use it

play01:36

with react or view or whatever you want

play01:38

to use it with to document your code

play01:40

alright so let's go ahead and NPM in it

play01:43

- why just to create a package Jason I'm

play01:47

gonna try to go kind of fast because I

play01:49

don't want the video to be too too long

play01:51

so let's NPM install as a dev dependency

play01:55

J s doc

play01:58

okay so that should get added to our

play02:01

package dot JSON file now jaya stock

play02:04

needs a config file actually you don't

play02:07

need to create it it will use defaults

play02:09

if you don't but we're gonna go ahead

play02:10

and create J s dot Jason and I'm gonna

play02:15

just just grab this real quick I'm gonna

play02:18

paste this in so I don't have to type it

play02:20

all out and just go over it so basically

play02:23

source is is where J s stock is gonna

play02:26

look and we're gonna have a folder

play02:27

called source you can have an array here

play02:29

of different folders if you want but

play02:32

we're gonna have one source folder we're

play02:34

gonna include anything that ends with

play02:35

dot J s and we're gonna exclude any node

play02:38

modules or Docs folders because Docs is

play02:41

what I used as the destination when it

play02:44

when it goes through and creates the

play02:45

documentation website it'll be put in

play02:48

this Doc's folder if you don't have this

play02:50

it'll just create a folder I believe

play02:52

called opt opt and then plugins so this

play02:55

plot will allow you allow you to use

play02:57

markdown this mono space clever links

play03:00

this is just going to make it so that

play03:02

when you have links within your

play03:03

documentation it'll have a mono space

play03:05

font and then recurse will allow us to

play03:08

basically recurse into subfolders okay

play03:11

so it'll look below just the root level

play03:14

so let's save this and then what we want

play03:17

to do is go to our package dot jason and

play03:19

create a script to run this you can also

play03:23

install it oops you can install it

play03:26

globally if you want but we're gonna

play03:31

just we're gonna do it local and just

play03:33

add a script so let's say J s doc and

play03:37

then we want to do the config file which

play03:39

is j j s dark jason okay so it always

play03:44

look at this config file for whatever

play03:46

settings we have alright and then let's

play03:49

create our source folder okay so this is

play03:52

where your actual code goes and we'll

play03:55

just have an index dot JSON i'm gonna

play03:58

run npm run doc

play04:01

all right so as you can see over here I

play04:05

created a folder called Docs and this is

play04:07

our documentation website it was just

play04:09

generated for us and we have no code

play04:11

right now but it still will generate and

play04:13

if we take take a look at the index.html

play04:16

it'll look like this by default and this

play04:20

is the default template there's a couple

play04:22

other templates you can use you can

play04:23

create your own templates if you want to

play04:26

change something like this home right

play04:28

here I'm going to show you how we can

play04:30

edit the default template without

play04:33

actually changing it in the node modules

play04:35

folder but we'll get to that in a little

play04:37

bit so let's go ahead and just add

play04:39

something to our index j/s so I mean I'm

play04:43

just gonna add a variable for now so

play04:45

it's a student name and let's set that

play04:48

to John Doe basically I want to add this

play04:54

to my documentation and I want to also

play04:57

add a type I want to use type checking

play04:59

to add a j/s doc comment envious code we

play05:03

can just do slash and then two asterisks

play05:05

and it will automatically finish for us

play05:07

and then we can hit enter and then add

play05:10

whatever j/s doc tags we want in this

play05:13

case we want to add a type and this is

play05:16

gonna be a string type we can also add a

play05:19

description here so we can say like

play05:21

student name which is pretty obvious and

play05:24

that's it so I'm gonna save this and go

play05:26

down here and generate our Doc's and

play05:29

then visit the website here and now

play05:32

you'll see we have global because we're

play05:34

in the global scope and then we have our

play05:37

student name variable or member and it

play05:40

gives us the name of it the description

play05:42

that we put the type which is string and

play05:45

then the source which is the file and

play05:46

the line number and I can actually click

play05:48

on the line and it will show me in my

play05:50

documentation that line highlighted okay

play05:53

so pretty cool and if we want this if we

play05:57

want to have actual type checking which

play05:59

I do have enabled you'll see if I change

play06:01

this to a one I'm gonna get this red

play06:04

squiggly line that says type 1 is not

play06:06

assignable to string so it works kind of

play06:09

like typescript now by default this

play06:11

isn't going to work what I what I've

play06:13

done is in my set

play06:15

if I go to my settings JSON file right

play06:20

here this is the this is what's allowing

play06:22

that to happen

play06:23

so JavaScript thought implicit project

play06:26

config guess is true if I set that to

play06:29

false or I don't have this at all then

play06:31

it's not going to do that so I couldn't

play06:33

show me that red line or anything all

play06:36

right it'll also show me in the property

play06:39

down I'll actually no it doesn't does it

play06:41

set that back to true yeah so now you'll

play06:47

see down here type 1 is not assignable

play06:49

to string all right now if you don't

play06:51

want to add that global setting what you

play06:54

can do is in your file you can add

play06:56

inside of a comment ts check like that

play06:59

and that will do the same thing without

play07:02

needing that setting all right so that's

play07:05

a string let's change it back to John

play07:09

Doe and you can do numbers boolean

play07:12

basically all the JavaScript types you

play07:15

can also do a raise in objects so I just

play07:17

want to show you that real quick let's

play07:19

say we have an array of grades so 98

play07:24

seven point seven decimals are numbers

play07:27

in JavaScript 76 and 89 if I want this

play07:31

to be an array we can go ahead and add a

play07:34

type not a type def we'll get into that

play07:37

after so type is gonna be array okay and

play07:41

for the description we'll say an array

play07:43

of grades okay now if I put in let's say

play07:50

a boolean here that's fine right now

play07:53

because I'm just saying it's type array

play07:55

but if we want it to be an array of

play07:56

numbers we can add some angle brackets

play08:00

and say number and now you can see I get

play08:02

an error that says type true is not

play08:05

assignable to number okay so if you know

play08:07

it's gonna be an array of numbers or

play08:09

strings or whatever you can put that in

play08:11

here now let's say we wanted an object

play08:15

like to do with an ID and a text value

play08:21

which is a string

play08:23

okay so if we want to do that in jaya

play08:28

stock we can add type and inside double

play08:33

curly braces we can say this has to have

play08:35

an ID that's a number and it has to have

play08:38

a text value that is a strength if I add

play08:43

something else in here I'm gonna get an

play08:46

error because it doesn't adhere to

play08:49

basically the model which is an ID and

play08:52

text just like you can do in typescript

play08:54

and then if I change let's see I'll

play08:57

change this to a string I'm gonna get an

play09:00

error because this is supposed to be a

play09:02

number now you can also have multiple

play09:04

types let's say this ID it can be a

play09:07

number or a string we can simply put a

play09:09

pipe character in here and put string

play09:11

and now it can be either so let's see

play09:14

let's save that and generate the

play09:18

documentation and now you can see we

play09:20

have our student named art to do gives

play09:23

us the type the description and the line

play09:25

number I actually didn't add a

play09:27

description here we'll just say to do

play09:30

object all right now before we move on

play09:34

to functions and params and stuff like

play09:36

that I want to show you how we can edit

play09:38

the template if we want so if we go into

play09:41

node modules and then J stock and then

play09:46

templates you'll see the default

play09:48

template that's used by default and we

play09:50

can describe what template we want to

play09:52

use from within our config but what I

play09:55

want to do is edit the default template

play09:57

and you never want to edit something in

play09:58

your node modules folder almost never so

play10:01

what we can do is just copy this folder

play10:04

this default and we can put it right in

play10:07

our route and let's just rename it to

play10:13

custom - template okay and then we can

play10:18

go to our config rjs doc dot jason and

play10:22

we can add in our opps right here we can

play10:24

add template and set it to custom

play10:29

template it's in the same directory so

play10:32

that's fine and now let's go into custom

play10:36

template and

play10:37

publish j/s and do a search for home and

play10:40

let's say we want to change this to what

play10:46

do we want to change is to just say Jas

play10:47

dark example but this home right here

play10:51

will also change to j a stock example or

play10:56

whatever you want and then i'll save and

play10:59

then let's regenerate our documentation

play11:01

and if we go back if we go right here j

play11:05

a stock example j a stock example okay

play11:09

and if you want to get rid of this

play11:11

there's different there's basically

play11:12

partials if you look in right here this

play11:17

TMP l there's all these TMP l files

play11:20

where you can you can edit those as well

play11:23

but you never want to edit the actual

play11:25

template in the node modules folder

play11:28

alright so let's see what else do you

play11:31

want to do let's move on to functions

play11:33

and params so let's say we have a

play11:36

function i'm actually gonna just just

play11:40

grab this real quick alright so it's

play11:43

just a very simple function to calculate

play11:44

tax and you can use an arrow function or

play11:48

you know regular function it doesn't

play11:51

matter it'll still document it the same

play11:53

way so it takes in an amount like 100

play11:56

and then a percentage like 50 actually

play11:59

lets to like point ten point one and

play12:03

we'll save this and just run the file so

play12:05

we can say node source slash index or

play12:09

since it's called index we can just do

play12:11

node source and we get a hundred and ten

play12:13

dollars now i want to document this so

play12:17

let's actually just get rid of the

play12:19

console.log so we'll put in a slash and

play12:23

then two asterisks and enter and notice

play12:25

that it actually looks at the params and

play12:28

creates the params with the name of the

play12:31

variables here and then we just want to

play12:34

change the type to number

play12:38

all right we'll give the function

play12:41

description will just say calculate tax

play12:47

but also you could put any function you

play12:50

want this is just something quick I just

play12:52

put in and then we can put a description

play12:55

for each parameter so we'll just say

play12:57

total amounts and this is the tax

play13:03

percentage okay and then with functions

play13:07

we can do a return or returns turns if

play13:13

it doesn't have a return value you can

play13:15

do void but this actually returns a

play13:18

string and it returns the total with a

play13:24

dollar sign okay so yeah because I'm

play13:28

returning a temple literal here with

play13:30

backticks

play13:31

and I have a dollar sign in front of the

play13:33

amount let's save that and let's run a

play13:38

documentation I'm sorry that's not right

play13:42

we want to run npm run dock and if we

play13:45

take a look now over here you'll see

play13:46

calculate tax now these are all our

play13:49

members basically our global variables

play13:52

and then we have methods okay so any

play13:54

methods we create will be put here we

play13:57

have calculate tax which returns a

play13:59

string we have the description and then

play14:02

it gives us this cool little table for

play14:03

parameters so the name the type and the

play14:07

description and then of course we can

play14:09

locate that function in our source code

play14:11

so this is great for someone that you

play14:14

know just I mean if you're working with

play14:16

someone else's code this really helps

play14:19

see what everything is what it does so I

play14:22

mean it can be really helpful when

play14:23

you're collaborating with other people

play14:26

so next thing I want to show you is how

play14:28

to create custom types something called

play14:31

a type def or a type definition so let's

play14:33

say we want to create a type called

play14:36

student so let's go down here and let's

play14:39

say inside our J a stock we'll say a

play14:43

student

play14:46

I'll just say yeah that's the student

play14:49

that's fine and then we want to use at

play14:52

type def okay so we're defining a type

play14:55

which is gonna be an object of and it's

play14:58

gonna be called student okay and then

play15:02

the student is gonna have certain

play15:04

properties so we'll say property give

play15:08

this property a type of number and we'll

play15:10

call this ID give it a description and

play15:13

we'll say students ID all right we'll

play15:18

just copy this down a couple times so

play15:22

this next one here is gonna be the name

play15:24

so say name and that's gonna be

play15:27

oops I did that wrong this should be

play15:29

string and this should be name student

play15:37

name let's do the age of the student so

play15:42

this will be and for this one I'll do a

play15:44

string or a number either one will be

play15:48

fine and I also want to make it optional

play15:49

if you want an optional value here you

play15:53

can put brackets around it okay so we'll

play15:55

say student age which is optional and

play16:00

then let's say let's do a boolean

play16:03

because we haven't done a boolean yet so

play16:06

we'll say boolean and we'll call this

play16:08

one is active so student is active and

play16:16

then down here let's go ahead and use it

play16:21

so we're gonna put in another j/s doc

play16:23

comment and we're gonna put in our type

play16:26

okay just like we did for our variables

play16:28

above except now instead of string or

play16:30

number or something like that we're

play16:32

gonna put in our student type all right

play16:34

and then let's say Const

play16:37

students

play16:39

and I'll say ID 1 oops ID 1 let's say

play16:45

name John Doe what else age 20 and is

play16:55

active true ok and everything is fine

play17:00

here now if I were to change let's see

play17:04

is active to a 1 we're gonna get an

play17:06

error because it's supposed to be a

play17:08

number I'm sorry it's supposed to be a

play17:10

boolean but it is a number if I get rid

play17:13

of his active we're gonna get an error

play17:15

because is active is not optional

play17:17

however if I get rid of age since we put

play17:20

brackets around it we're not gonna get

play17:22

warning ok

play17:24

and again it's up to you if you want to

play17:25

use it for type checking or not and you

play17:28

know if you want to enable that that

play17:29

functionality but let's go ahead and

play17:32

generate our Docs and let's take a look

play17:36

over here and we have student okay we

play17:40

also have student name so student name

play17:43

is the member right here and if we look

play17:46

at the type well I guess oh wait no it's

play17:49

not student name student that's right

play17:52

here a student and it has the type of

play17:56

student okay see that and then if we go

play17:59

down actually wait a minute all right

play18:05

it's right in front of my eyes type

play18:07

definition so we have a student type

play18:11

ultimately it's an object but our

play18:13

properties here ID number and you can

play18:18

have attributes as well so ID name age

play18:22

which is optional is active and then if

play18:25

we want to locate that type def it'll

play18:28

take us to that in our documentation so

play18:31

now what I want to do is give you an

play18:32

example of a class okay cuz up to this

play18:34

point we've been just kind of working in

play18:36

the like the global scope so let's

play18:40

create a class

play18:41

and we'll call this person just

play18:45

something very simple and we want to

play18:50

give this basically just a description

play18:53

so we'll say class to create a person

play18:58

object and then let's have a constructor

play19:03

and I mean I'm not going to explain what

play19:08

a class is or construct or anything like

play19:10

that this is geared towards people

play19:13

people that already know that if you

play19:14

don't I would suggest watching either my

play19:17

JavaScript crash course on YouTube or my

play19:19

modern JavaScript from the beginning on

play19:21

you to me

play19:22

so constructor let's say it takes in an

play19:25

object called person info so we can put

play19:29

here a per am tag so if we put our j/s

play19:32

doc here we can say per am object person

play19:37

info we can put a description here if we

play19:39

want like in for information about the

play19:44

person and in our constructor let's take

play19:49

say this dot name will create a name

play19:52

property and set it to that person info

play19:55

name and then let's take this dot age

play20:00

and set that to the person info dot age

play20:05

okay and then we can adhere our jeaious

play20:09

doc and we can add a property so let's

play20:13

say property because it's a property of

play20:16

a class it's a string of name they'll

play20:18

say person's name alright we could do

play20:22

the same thing for age so person's age

play20:31

age okay so that's our constructor and

play20:35

then let's just create we'll just create

play20:38

a method here and let's call this greet

play20:41

and we're just gonna do a console log

play20:44

here of a template string and let's say

play20:48

hello my name is and we can say this dot

play20:53

name and I am this dot age so I kind of

play21:01

a silly example but it's just to kind of

play21:03

show you how to use jeaious dock so

play21:06

above greets so above any method we can

play21:09

go ahead and put in here a property and

play21:20

this is a function called greet say a

play21:25

greeting with the name and age and then

play21:30

we can put a returns okay so this is

play21:34

going to return void because it's just a

play21:37

console log so it's not going to return

play21:38

anything okay so let's see well go ahead

play21:42

and save that and let's run our dock and

play21:48

you can see it actually created a new

play21:51

file called person HTML so any class you

play21:53

create it will create that file and if

play21:55

we check it out notice that now in

play21:57

addition to just our global scope we

play21:59

have classes and if we click on person

play22:02

this gives us all the information about

play22:04

our person class gives us the

play22:06

description so the constructor the

play22:10

parameters for the constructor which is

play22:12

the person info which is an object we

play22:15

have the members which is an age named

play22:17

methods we only have one method called

play22:20

greet and we can go to it here if we

play22:23

want and I just want to show you real

play22:25

quick link so let's say we have let's

play22:28

just it initialize a person here so

play22:31

we'll say we'll call this person one

play22:33

said it's a new person okay so we're

play22:36

just using our person class and remember

play22:39

it takes in an object

play22:40

with a name and an age and then I don't

play22:49

know just to a console log here of

play22:51

person one dots greet and we'll just run

play22:56

the file see if that works so node

play22:58

source we get hello my name is John Doe

play23:01

and I am 30 not sure what the undefined

play23:05

is actually I don't know but that's fine

play23:10

it doesn't really matter what I want to

play23:12

show you here is we can actually link to

play23:17

stuff so we can say like C let's say

play23:21

person person one but I just want to

play23:25

show you we can do C and then we can use

play23:28

link like this link and then we want to

play23:34

link to the person class because this

play23:36

variable is related to that person class

play23:39

so if I go ahead and generate our Doc's

play23:42

and now we take a look over here you'll

play23:47

see person 1 and right here person 1 the

play23:52

description and C person so if I click

play23:54

on that it'll actually take me to the

play23:56

class so you can link to other parts of

play23:59

the documentation you know from certain

play24:02

parts which is pretty cool

play24:04

alright I'll probably just get rid of

play24:06

that so I want to do that so let's do

play24:12

modules so a lot of times you might have

play24:14

separate modules and this works with

play24:15

both common j/s which is you know when

play24:18

you're working in nodejs and you use the

play24:20

require syntax or es2015 modules when

play24:23

you use import/export you know if you're

play24:26

in react or view or wherever you want to

play24:28

use es2015 modules so i'm gonna create a

play24:31

new file here called in my source folder

play24:34

called calculator dot j/s and it's just

play24:40

going to be a bunch of methods so I'm

play24:42

going to

play24:44

let's say yeah I'm just gonna paste this

play24:49

in just give me a second all right so

play24:56

I'm gonna paste this in and just go over

play24:57

it

play24:58

so when we create another file basically

play25:01

that we're gonna bring in it's a module

play25:03

and we can just define it with the

play25:06

module tag here calculator give it a

play25:08

description and then I just have a you

play25:11

know add multiply subtract and divide

play25:13

just as separate functions I'm using

play25:16

shorthand arrow functions syntax and I

play25:19

just have just like I showed you before

play25:21

just the params and then the return okay

play25:24

so nothing new here it's just all in a

play25:26

module and we just called it calculator

play25:29

so let's save this and let's go to index

play25:32

J s and bring this in up top so I'm I'm

play25:36

using node so I'm gonna use the common

play25:39

J's syntax so we'll say Const and then

play25:41

we'll just bring in we'll use D

play25:43

structuring to bring in all the methods

play25:46

from calculator or actually I'm sorry we

play25:54

need to do dot slash calculator okay so

play25:58

we can bring in you know add subtract

play26:02

divide and multiply and then if we want

play26:07

to use it we can go down here and we'll

play26:11

just say I don't know console.log and we

play26:16

can take add we'll add 20 and 50 and

play26:21

notice that it gives me that checking

play26:24

okay so it tells me that it needs two

play26:27

arguments so say 20 and 30 and I'll just

play26:32

run this node source and we get 50 okay

play26:37

now let's generate our documentation and

play26:40

take a look let's just reload this

play26:43

actually let's go to

play26:46

okay so over here now you'll see we have

play26:49

modules so for every module you create

play26:52

whether it's es2015 or common j/s it'll

play26:56

show that the module here and if I click

play26:58

on it we get all the information about

play27:00

that module so the description the

play27:03

source all the methods along with their

play27:07

parameters okay so you can see you know

play27:10

number and one and two first number

play27:13

second number and it just basically

play27:15

documents that module okay just like a

play27:18

documents a class and then any global

play27:20

entities that we have here so it's

play27:23

pretty cool I really like this I think

play27:26

it's it is very verbose I mean you know

play27:29

having to put all these comments and but

play27:32

it makes things clearer even just in the

play27:36

code you know so you know what's what so

play27:39

I mean there's pros and cons to it my

play27:41

job isn't to try to convince you to use

play27:43

it it's to just show it to you and then

play27:45

you take it upon yourself if you want to

play27:48

use it or not all right so let's see

play27:51

what else do I want to show you guys

play27:53

let's do let's look at tutorials because

play27:56

you can actually add tutorials to your

play27:58

documentation so what we can do is go to

play28:00

our Jaso Jason and from here we can add

play28:10

tutorials and then we can have a folder

play28:13

where we want to display our tutorials

play28:15

which is gonna be tutorials

play28:18

alright actually let's do dot slash I

play28:22

should probably do that yeah so inside

play28:26

our root here we're gonna create a

play28:30

folder called tutorials

play28:34

now tutorials can be either HTML or

play28:38

markdown I believe they can be XML

play28:40

there's a there's a couple different

play28:42

formats you can use but I'm gonna create

play28:44

a new file here and I'm gonna call it

play28:48

program - tutorial dot HTML and then

play28:54

it'll just generate an HTML file and

play28:57

let's say program tutorial and we'll

play29:04

just put a paragraph here with a bunch

play29:09

of dummy text alright so I'll save that

play29:12

let's create one more so this one let's

play29:16

say calculator tutorial and this would

play29:22

let's just make this one markdown just

play29:25

to show you so I'll do dot MD and then

play29:28

I'll just put in I'm just gonna paste in

play29:31

a bunch of text here as well and

play29:35

obviously you can use you know all kinds

play29:36

of markdown so we have two tutorials now

play29:40

I'm gonna go ahead and generate my Docs

play29:42

and let's take a look and how we have

play29:45

tutorials we have our calculator

play29:48

tutorial our program tutorial now this

play29:51

doesn't look too good this title here

play29:53

it's basically just it's going to use

play29:55

the name of the file by default but what

play29:58

we can do is inside of the tutorials

play30:01

folder we can create a file called

play30:05

tutorials dot jason and in here let's go

play30:11

ahead and open up some curly braces and

play30:12

we can put program basically the name of

play30:16

the file tutorial and set that to an

play30:21

object and then we can have title

play30:26

program title calculator tutorial and

play30:36

set that to have title tutorial alright

play30:49

so save that and to generate the doc

play30:53

again and check it out and look at that

play30:56

program title calculator wait wait I put

play31:00

program title should be program tutorial

play31:07

or whatever you want it doesn't really

play31:09

matter just showing you how to do it

play31:11

alright so that's pretty cool now let's

play31:14

say in our calculator j/s we actually

play31:18

want to point to that tutorial so we can

play31:22

go ahead and in the description here

play31:24

let's uh oops the hell I just do let's

play31:31

go up to the description here and let's

play31:33

add C and we can actually open up some

play31:39

curly braces and we can put at tutorial

play31:43

and then we can point to the calculator

play31:47

- tutorial and now I'm gonna generate

play31:52

the docs go back and now if I go and I

play31:56

look at my calculator module you can see

play31:59

right here see calculators tutorial and

play32:02

it will point me to that tutorial

play32:04

alright so obviously you know this is

play32:07

just a mess of code it's not it's not an

play32:09

actual program or anything but you can

play32:12

really structure this to make a lot of

play32:14

sense and really help you know other

play32:17

people out as well as yourself a lot of

play32:19

times we write code we don't look at it

play32:21

for a few months and then we go back and

play32:23

we're like what the hell does this even

play32:24

do you know so this will document

play32:27

everything for you which is really cool

play32:28

in addition to that it'll give you some

play32:32

type checking functionality all right so

play32:35

I think we're just about done here let's

play32:38

go back to the

play32:38

home now for this area here there's a

play32:42

few things we can do we can actually add

play32:44

in let's go back to index j s and i'll

play32:47

have this code in the description I'm

play32:49

not sure what I'm gonna put it in it

play32:50

might be a github repo or it might be

play32:53

like a plunker or something but here

play32:57

let's add C we'll go right here and we

play33:03

can actually put in some J s stock and

play33:05

we can use at file and this so there's

play33:08

so many of these tags that I'm not gonna

play33:09

go over that you can check out in the

play33:11

documentation but let's just say index J

play33:15

s is the root file for this example app

play33:23

and we can actually put other things

play33:26

like author we can put if we want maybe

play33:33

we want to point to an external website

play33:34

we could do C and we can put a link in

play33:38

here like this just put my website here

play33:51

alright so maybe you have a you know a

play33:53

website you want to put in here stuff

play33:55

like that and if I save this and then

play33:57

run it take a look now we have this here

play34:01

it gives you it'll have the file name

play34:03

and then the description author source

play34:08

and the website alright and another

play34:13

thing we can do is we can actually add a

play34:14

readme here if we want to so and you can

play34:18

directly edit the template as well as

play34:20

long as it's not the actual template in

play34:22

the node modules folder but let's say we

play34:25

have we create a readme file you don't

play34:29

have to do all this stuff I'm just kind

play34:31

of showing you guys that you you know

play34:32

some of your options that you have so

play34:34

we'll just say this is just a sample

play34:41

script how to use J s doc

play34:49

okay so we'll save this and then what we

play34:53

can do is go to our config file and in

play34:57

this config file we can add a readme

play35:02

obviously your readme would probably

play35:04

have a lot more than that but just to

play35:06

give you an example and then we're just

play35:09

going to point to that file so readme

play35:12

dots MD okay so let's run this and take

play35:17

a look and there it is so this comes

play35:20

from our readme file so we've built this

play35:23

documentation for just a nonsense

play35:26

program but you can see all of all the

play35:29

different things that you can do with it

play35:31

and there's a lot more I'm just getting

play35:33

into kind of the fundamentals of j/s doc

play35:36

now if you're really interested in

play35:39

jeaious doc and you want to start using

play35:40

it what I would do is I'm actually going

play35:43

to assign you some homework so take

play35:45

something you've already built some kind

play35:46

of JavaScript program it could be

play35:48

something tiny it could have been a

play35:49

tutorial that you followed on my channel

play35:52

or someone else's it could be a large

play35:54

project and implement jeaious talk into

play35:56

it install it set up the config file and

play36:00

add your j/s doc comments where neat

play36:04

where you want and create some

play36:06

documentation because as I said in my

play36:09

last video about tutorial hell just

play36:11

following along isn't enough you need to

play36:13

dive in and do stuff on your own so I

play36:16

think that's a nice little homework

play36:18

assignment is to take something you've

play36:20

already built and just implement J has

play36:22

docked comments and generate some

play36:24

documentation alright guys that's it

play36:26

hopefully you enjoyed this if you did

play36:28

please leave a thumbs up and I'll see

play36:29

you next time

Rate This

5.0 / 5 (0 votes)

関連タグ
JSDocDocumentationTypeScriptNode.jsCode TutorialWeb DevelopmentAPI ReferenceSource CodeType CheckingDevTools
英語で要約が必要ですか?