-
Notifications
You must be signed in to change notification settings - Fork 2
/
js注释.txt
72 lines (43 loc) · 1.37 KB
/
js注释.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
1.JavaScript 注释用于解释 JavaScript 代码,增强其可读性。
JavaScript 注释也可以用于在测试替代代码时阻止执行。
2.单行注释
单行注释以 // 开头。
任何位于 // 与行末之间的文本都会被 JavaScript 忽略(不会执行)。
下例在每条代码行之前使用单行注释:
实例
// 改变标题:
document.getElementById("myH").innerHTML = "我的第一张页面";
// 改变段落:
document.getElementById("myP").innerHTML = "我的第一个段落。";
下例在每行结尾处使用了单行注释来解释代码:
实例
var x = 5; // 声明 x,为其赋值 5
var y = x + 2; // 声明 y,为其赋值 x + 2
3.多行注释
多行注释以 /* 开头,以 */ 结尾。
任何位于 /* 和 */ 之间的文本都会被 JavaScript 忽略。
本例使用多行注释(注释块)来解释代码:
实例
/*
下面的代码会改变
网页中
id = "myH" 的标题
以及 id = "myP" 的段落:
*/
document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
注释:使用单行注释最为常见。
提示:注释块常用于官方声明。
4.使用注释来阻止执行
使用注释来防止代码执行很适合代码测试。
在代码行之前添加 // 会把可执行的代码行更改为注释。
下例使用 // 来阻止代码行的执行:
实例
//document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
下例使用注释块来阻止多行代码的执行:
实例
/*
document.getElementById("myH").innerHTML = "我的第一张页面";
document.getElementById("myP").innerHTML = "我的第一个段落。";
*/