Home > Web Front-end > JS Tutorial > body text

What is JSDoc? Introduction to JSDoc annotation specifications

不言
Release: 2019-01-25 11:52:29
forward
3415 people have browsed it

The content of this article is about what is JSDoc? The introduction of JSDoc annotation specifications has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is JSDoc

JSDoc is a tool that generates API documentation for JavaScript applications or modules based on annotation information in JavaScript files. You can use JSDoc tags such as: namespace, class, method, method parameters, etc. This enables developers to easily read the code and master the classes defined by the code and their properties and methods, thereby reducing maintenance costs and improving development efficiency.

JSDoc comment rules

JSDoc comments should generally be placed before a method or function declaration, and it must start with /** in order to be recognized by the JSDoc parser. Any other comments starting with /* , /*** or with more than 3 asterisks will be ignored by the JSDoc parser. As shown below:

/*
**一段简单的 JSDoc 注释。
*/
Copy after login

The comment effect of JSDoc

If we have a piece of code like this without any comments, it seems that there is a certain cost.

functionBook(title, author){
      this.title=title;
      this.author=author;
}
Book.prototype={
      getTitle:function(){
        returnthis.title;   
      },
setPageNum:function(pageNum){
      this.pageNum=pageNum;   
}
};
Copy after login

If you use JSDoc to annotate the code, the readability of the code will be greatly improved.

/**
* Book类,代表一个书本.
* @constructor
* @param {string} title - 书本的标题.
* @param {string} author - 书本的作者.
*/
functionBook(title, author){
    this.title=title;
    this.author=author;
}
Book.prototype={
/**
* 获取书本的标题
* @returns {string|*} 返回当前的书本名称
*/
getTitle:function(){
    returnthis.title;   
},
/**
* 设置书本的页数
* @param pageNum {number} 页数
*/
setPageNum:function(pageNum){
    this.pageNum=pageNum;   
}
};
Copy after login

@constructor Constructor declaration annotation

@constructor makes it clear that a function is the constructor of a certain class.

@param Parameter annotation

We usually use @param to represent the parameters of functions and class methods. @param is the most commonly used annotation tag in JSDoc. Parameter labels can represent a parameter's parameter name, parameter type, and parameter description comments. As shown below:

/**
* @param {String} wording 需要说的句子
*/
functionsay(wording){
    console.log(wording);
}
Copy after login

@return Return value comment

@return indicates the return value of a function. If the function does not display the specified return value, it does not need to be written. As shown below:

/*
* @return {Number} 返回值描述
*/
Copy after login

@example Example comment

@example is usually used to represent example code. Usually the code of the example will be written on a new line, as shown below:

/*
* @example
* multiply(3, 2);
*/
Copy after login

Other common comments

@overview Description of the current code file.

@copyrightThe copyright information of the code.

@author [] Author information of the code.

@versionThe version of the current code.

The above is the detailed content of What is JSDoc? Introduction to JSDoc annotation specifications. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template