如何在 JavaScript 中编写注释

介绍

在编程中,我们的第一个考虑通常是机器 - 计算机是如何阅读和解释我们编写的代码. 然而,同样重要的是考虑谁会阅读和使用代码。

评论是程序源代码中的注释,被解释者忽略,因此对代码的实际输出没有影响。

作为一名开发人员,沉浸在没有被正确评论的其他人撰写的代码中可能令人沮丧,而当你不再沉浸在程序的背景下时,很容易忘记自己的代码意味着什么。

如何合成

让我们快速看看 JavaScript 评论语法的两种不同类型。

单行的评论是用两条前缀(//)写的:

1// This is a comment

直至行末尾的所有字符将被JavaScript忽略。

Block评论,有时称为 mutli-line评论,是用开头标签(/*)和关闭标签(*/)写的。

1/* This is
2a comment */

上面的代码块中的打开和关闭标签之间的一切都将被忽略。

单行和多行评论都写在它们被指定解释的代码上,如下Hello, World!示例所示:

1[label hello.js]
2// Print "Hello, World!" to the console
3console.log("Hello, World!");

写评论时,请将其输入到相同的层面,如下面的代码:

1[label ocean.js]
2// Initialize a function
3function alphabetizeOceans() {
4    // Define oceans variable as a list of strings
5    const oceans = ["Pacific", "Atlantic", "Indian", "Antarctic", "Arctic"];
6
7    // Print alphabetized array to the console
8    console.log(oceans.sort());
9}

请注意,评论就像程序本身一样是代码的一部分。过时的评论可能比没有评论更有害,所以请记住定期维护和更新评论以及其他一切。

内地评论

单行评论被称为 inline评论,当它们出现在代码行末尾时。

1let x = 99;    // assign numerical value to x
2let y = x + 2; // assign the sum of x + 2 to y

内线评论可以用于快速注释小,具体的内容片段. 因为评论应该只与它写的确切行有关,这是最明显的评论类型。

请记住,没有办法在行上结束一个单行评论,所以请确保在//语法之后不放任何代码,如下示例所示。

1[label broken.js]
2for (let i = 0; i === 10; i++) // for loop that runs ten times {
3    // Running this code results in a syntax error
4}

虽然内线评论可能有用,但应该谨慎使用 - 包含大量内线评论的代码很快就会变得混乱,因此很难阅读。

区块链评论

区块级评论,或多行评论,是用于介绍和解释代码的一部分的长形式注释,通常这些类型的评论被放置在文件的顶部,或在一个特别复杂的代码块之前。

 1[label greet.js]
 2/* Initialize and invoke a the greetUser function
 3to assign user's name to a constant and print out
 4a greeting. */
 5
 6function greetUser() {
 7    const name = prompt("What is your name?");
 8    console.log("Hello ," + name + "! How are you?");
 9}
10
11greetUser();

您还可能有时会看到区块评论语法的略有修改版本,该语法以/**开始,并在评论区的左侧包含星座。

 1[label sea.js]
 2/**
 3 * Initialize constant with an array of strings.
 4 * Loop through each item in the array and print
 5 * it to the console.
 6 */
 7
 8const seaCreatures = ["Shark", "Fish", "Octopus"];
 9
10for (const seaCreature of seaCreatures) {
11  console.log(seaCreature);
12}

有时,此类评论还会包含有关编程文件的详细信息,包括脚本的名称、版本和作者。

如果你是JavaScript的初学者,你可以写尽可能多,以便学习和理解你写的代码. 随着你作为JavaScript开发人员的进步,你将寻求回答代码背后的意图或为什么,而不是如何或什么。

评论出测试代码

评论也可以用来快速轻松地防止执行代码进行测试和调试,这被称为评论代码

如果您所编写的任何代码中有错误,评论部分将防止它们运行,并有助于确定问题来源,您也可以使用它来切换代码以测试不同的结果。

 1[label math.js]
 2// Function to add two numbers
 3function addTwoNumbers(x, y) {
 4  let sum = x + y;
 5  return sum;
 6}
 7
 8// Function to multiply two numbers
 9function multiplyTwoNumbers(x, y) {
10  let product = x * y;
11  return product;
12}
13
14/* In this example, we're commenting out the addTwoNumbers
15function, therefore preventing it from executing. Only the
16multiplyTwoNumbers function will run */
17
18// addTwoNumbers(3, 5);
19multiplyTwoNumbers(5, 9);

单行评论和封锁评论都可以用来评论代码,取决于被转移的部分的大小。

<$>[注] :评论代码只能在测试过程中完成。

在制定程序的逻辑时,评论代码可以帮助您确定错误的位置或评估最有用的代码行。

结论

JavaScript 代码是由计算机解读的,但它总是会被其他程序员阅读,包括你未来的自己. 花时间在代码的复杂部分上留下适当的注释将在未来支付股息,使你和合作者更容易理解你写的代码的意图。

Published At
Categories with 技术
comments powered by Disqus