JavaScript基本语法指南:注释用法实例

2021年3月17日14:33:38 发表评论 686 次浏览

注释用于防止执行语句。编译器执行代码时, 注释将被忽略。注释是用户友好的, 因为用户可以使用注释获得代码的解释。

语法如下:

// For single line comment
/* For block of lines comment
...
...
*/

返回值:在执行代码期间, 注释将被忽略。

范例1:此示例说明了使用//的单行注释。

<!DOCTYPE html>
< html >
  
< head >
     < title >
         JavaScript Comments
     </ title >
      
     < script >
          
         // Function to add two numbers
         function add() {
              
             // Declare three variables
             var x, y, z;
              
             // Input a number and store it into variable x
             x = Number( document.getElementById("num1").value );
              
             // Input a number and store it into variable x
             y = Number( document.getElementById("num2").value );
              
             // Sum of two numbers
             z= x +y;
              
             // Return the sum
             document.getElementById("sum").value = z;
         }
     </ script >
</ head >
  
< body >
     Enter the First number: < input id = "num1" >< br >< br >
     Enter the Second number: < input id = "num2" >< br >< br >
      
     < button onclick = "add()" >
         Sum
     </ button >
      
     < input id = "sum" >
</ body >
  
</ html >

输出如下:

在单击按钮之前:

JavaScript 注释1

单击按钮后:

JavaScript 注释2

范例2:此示例说明了使用/ *…* /的多行注释

<!DOCTYPE html>
< html >
  
< head >
     < title >
         JavaScript Comments
     </ title >
      
     < script >
      
         /* Script to get two input from user
         and add them */
         function add() {
              
             /* Declare three variable */
             var x, y, z;
              
             /* Input the two nos. num1, num2
             Input num1 and store it in x
             Input num2 and store it in y
             The sum is stored in a variable z*/
             x = Number(document.getElementById("num1").value); 
             y = Number(document.getElementById("num2").value);
             z = x + y;
              
             document.getElementById("sum").value = z;
         }
     </ script >
</ head >
  
< body >
     Enter the First number : < input id = "num1" >< br >< br >
     Enter the Second number: < input id = "num2" >< br >< br >
      
     < button onclick = "add()" >
         Sum
     </ button >
      
     < input id = "sum" >
</ body >
  
</ html >

输出如下:

在单击按钮之前:

JavaScript 注释3

单击按钮后:

JavaScript 注释4

范例3:此示例说明注释的代码将永远不会执行。

<!DOCTYPE html>
< html >
  
< head >
     < title >
         JavaScript Comments
     </ title >
      
     < script >
          
         function add() {
              
             var x, y, z;
              
             x = Number( document.getElementById("num1").value );
              
             y = Number( document.getElementById("num2").value );
              
             z= x +y;
              
             // Comment the code section
             //document.getElementById("sum").value = z;
         }
     </ script >
</ head >
  
< body >
     Enter the First number: < input id = "num1" >< br >< br >
     Enter the Second number: < input id = "num2" >< br >< br >
      
     < button onclick = "add()" >
         Sum
     </ button >
      
     < input id = "sum" >
</ body >
  
</ html >

输出如下:

在单击按钮之前:

JavaScript 注释5

单击按钮后:

JavaScript 注释6

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: