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

In-depth understanding of JS code annotation methods and code annotation specifications

yulia
Release: 2018-10-10 14:35:52
Original
9231 people have browsed it

During page layout, in order to improve the readability of the code, it is often necessary to add comments to the code. This article will talk to you about how to annotate JS code and the specifications of JS code comments. Interested friends can refer to it. Hope this helps!

Comments are an essential part of the code, and a complete program must be commented. Comments not only help you update and modify the project, but also help others know what you wrote when they get your project.

1. JavaScript code comment method

JavaScript will not execute the code in the comment. JS code comments are divided into single-line comments and multi-line comments.

1. Single-line comments

Single-line comments start with //, add a space after //, and then write the content of the comment. Single-line comments can be used to describe the function of the current code and explain the code etc.

Code example: Comment a single line of the document.write("String is:" str) code without requiring it to be displayed on the page

<script type="text/javascript">
  function myFunction(){
   var animal = ["dog", "cat", "elephant", "tiger"];
   var str=animal.toString();
   document.write("类型是:"+typeof(str));  
   //document.write("字符串是:"+str)
  }  
 </script>
Copy after login

Rendering before comment:

In-depth understanding of JS code annotation methods and code annotation specifications

Rendering after commenting:

In-depth understanding of JS code annotation methods and code annotation specifications

2. Multi-line comments

Multi-line comments start with /* and end with */ at the end, but try to use single-line comments instead of multi-line comments in your work. Even if the comment requires multiple lines, it should be completed with a single-line comment. However, it is recommended to use multi-line comments for function comments.

Code example:

<script>
/*
下面的这些代码会输出
一个标题和一个段落
并将代表主页的开始
*/
document.getElementById("myH1").innerHTML="欢迎来到菜鸟教程";
document.getElementById("myP").innerHTML="这是一个段落。";
</script>
Copy after login

2. Specifications for JS code comments

A good code comment should be done, if it is not necessary , do not add comments. If you need to add comments, write them in detail to let others understand what you mean, and try to improve the clarity and readability of the code. The following points should be achieved during work:

1. In logical code, code comments should be more than 20%

2. When file comments are made, file comments should be written into the file. Header

3. When commenting on classes and interfaces, the comment needs to be placed before class and after the using or package keyword

4. When encountering class attributes, public and protected methods, comment It needs to be written on class attributes, public and protected methods. That is, use // comments, which need to be aligned with the commented code

5. Comments need to be placed in front of the commented code, written in separate lines and do not leave blank lines in the middle

6. Do not put them in comments Use abbreviations. If you want to use abbreviations, you need to provide necessary explanations for the abbreviations.

7. When commenting on member variables, use // to indicate the meaning, purpose and function of the member variables, and where they may be used. Comments need to align the commented code

In short, every programmer should develop a good habit of writing comments, which not only makes it easier for yourself to read and modify the code, but also makes it easier for others to understand and maintain your code.

The above introduces the annotation method of JS code and the annotation specifications of JS code. It is relatively simple and easy to understand. For more related tutorials, please visit JavaScript Video Tutorial

The above is the detailed content of In-depth understanding of JS code annotation methods and code annotation specifications. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!