-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter4.html
65 lines (51 loc) · 1.36 KB
/
chapter4.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chapter4</title>
</head>
<body>
</body>
</html>
<script>
console.log("理解基本类型和引用类型的值")
// 不能给基本类型的值添加属性
var name = "Nicholas";
name.age = 27;
alert(name.age);
// 复制基本类型变量
var num1 = 5;
var num2 = num1;
// 改变num1的值,2不变
num1 = 6;
alert("num2=" + num2)//num2=5
//复制引用类型变量
var obj1 = new Object();
var obj2 = obj1;
obj1.name = "Nicholas";
alert("obj2.name=" + obj2.name); //"Nicholas"
//typeof 和instanceof 需要注意的
alert("typeof 和instanceof 需要注意的")
alert(typeof (null))
alert(null instanceof Object)
alert(" 延长作用域链")
function buildUrl() {
var qs = "?debug=true";
var loc = {}
with (loc) {
var url = location.href + qs;
}
return url;
}
alert(buildUrl())
alert("但在 JavaScript 中,if/for 语句中的变量声明会将变量添加到当前的作用域")
if (true) {
var color = "red";
}
alert(color)
for (var i = 0; i < 10; i++) {
}
alert(i)
//手动赋值null,以求脱离执行环境。让垃圾回收
i=null;
</script>