JavaScript注释教程与示例

Comments are used to explain code and make it more readable by others in JavaScript programming language. Comments can be a single line, multiline or inline. Another useful usage of the comments is creating testing code and executing or preventing the execution of the testing code.

注释用于解释代码,并使其他人可以用JavaScript编程语言阅读。 注释可以是单行,多行或内联。 注释的另一个有用用法是创建测试代码并执行或阻止执行测试代码。

单行注释 (Single-Line Comments)

Single line comments only comment on the used line. Double slash // is used to create the single-line comment and from the position // to the end of the line will be marked as a comment and it is omitted by the browser. But the commented line is readable by others even it is not executed and omitted. Also, most of the editors will make the comments a different color like green which is not the same as the JavaScript code.

单行注释仅对使用的行进行注释。 双斜杠//用于创建单行注释,并且从//位置到行尾将被标记为注释,浏览器将其省略。 但是注释行即使不执行和省略也可以被其他人读取。 另外,大多数编辑器会将注释设为不同的颜色,例如绿色,这与JavaScript代码不同。

// This is a single line comment

alert("This is a JavaScript alert which is working");

// THIS IS A SINGLE LINE COMMENT

alert("This is a JavaScript alert which is working");

// 123456789 This is a comment too.

alert("This is a JavaScript alert which is working");

单行内联注释 (Single Line Inline Comments)

We can use the single-line comment as an inline comment in the single line. An inline comment is a comment which is added to the code line where the code will be executed but the comment will not execute. From the start of the inline comment to the end of the line will be a comment. But from the start of the line to the start of the inline comment will be a code line that will be executed.

我们可以将单行注释用作单行中的嵌入式注释。 内联注释是添加到将在其中执行代码但不会执行该注释的代码行的注释。 从内联注释的开始到行尾将是一个注释。 但是从行的开始到内联注释的开始将是将要执行的代码行。

var age = 12;  // This is single line inline comment and the new variable age will be created

var new_age = age + 20; // This single line inline comment will not affect to sum of age with 20

多行注释 (Multi-line Comments)

Multi-line comments can be used to put more comments or explanations about the code. Multi-line comments also used to provide more details about the function, classes, etc. In order to create a multi-line comment, we should set the start and end of the multiline comment. /* is used to set the start f the multi-line comment and */ is used to set the end of the multi-line comment. Between /* and */ are interpreted as a comment.

多行注释可用于放置有关代码的更多注释或解释。 多行注释还用于提供有关函数,类等的更多详细信息。为了创建多行注释,我们应设置多行注释的开始和结束。 /*用于设置多行注释的开始,而*/用于设置多行注释的结束。 /**/被解释为注释。

/* This is
Multi line comment
*/

alert("Test");

/*
This 
is 
Multi-line
Comment
Too
*/

用单行注释阻止代码执行 (Prevent Code Execution with Single Line Comments)

While testing some JavaScript code single line comment can be used to test some code and then prevent the execution of the given code line. In the following example, we will prevent the alert() function to print screen Hi which is done by the single-line comment. We can enable this code line just by uncommenting the code line.

在测试某些JavaScript代码时,单行注释可用于测试某些代码,然后阻止执行给定的代码行。 在下面的示例中,我们将防止alert()函数打印由单行注释完成的屏幕Hi 。 我们只需取消注释代码行即可启用此代码行。

// alert("Hi");

使用多行注释防止代码执行 (Prevent Code Execution with Multi-Line Comments)

We can also use multi-line comments in order to prevent some code from execution. This can be useful for more complex test scenarios. In the following example, we will comment on multiple line codes to prevent the execution.

我们也可以使用多行注释,以防止执行某些代码。 这对于更复杂的测试方案很有用。 在下面的示例中,我们将注释多个行代码以防止执行。

/*

alert("Hi");

callThisFunction();

*/

alert("Merhaba");

callThatFunction();

In this example the alert("Merhaba"); and callThatFunction() will be executed properly where alert("Hi") and callThisFunction() will not executed because they are commented or just comments.

在此示例中, alert("Merhaba");callThatFunction()将在不执行alert("Hi")callThisFunction()情况下正确执行,因为它们已被注释或仅被注释。

LEARN MORE  Java - Basics and Comments
了解更多Java-基础知识和注释

翻译自: https://www.poftut.com/javascript-comments-tutorial-with-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值