JavaScript速成
# JavaScript速成
解释性语言(边执行边编译)
运行在客户端
面向对语言
# 三种添加JavaScript的方法
# 内部的JavaScript:
<script>
//代码
</script>
1
2
3
2
3
# 外部的JavaScript:
<script src="script.js"></script>
1
# 内联JavaScript:(不推荐)
<button onclick="createParagraph()"></button>
1
JavaScript不区分整数和浮点数类型(都是浮点数)
var string = "abcde"
console.log(string)
let a = 1
console.log(a)
string="modify"
console.log(string)
a = 1+1
console.log(a)
const PI = 3.14
// PI = 3 会报错,不能改变
console.log(PI)
var x = 5
console.log(x+5)
x = x+5
x += 5
x++
console.log(x)
x = "3" + "4"
console.log(x)
x = "3" + 4 + 5
console.log(x) //345
x = 3 + 4 + "5" //75,先算3+4 = 7 然后7+"5"
console.log(x)
console.log( 123 == "123") //true
console.log(123 === "123") //false
console.log( 1 == true) //true
console.log(1 === true) //false
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
72
73
74
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
72
73
74
# 循环
var name = "zhangsan"
var age = 20
if (name == "zhangsan" && age == 20 ){
console.log('zhangsan')
}else if(name == "lisi"){
console.log('lisi')
}else{
console.log("error")
}
// while(true){
// console.log("true")
// }
a = 19
do{
a++
console.log(a)
} while(a<20)
if (true){
// var b = 20
// let b = 20 作用范围在当前作用域
}
a = 20
// if (b == a){
// console.log('var没有作用范围')
// }
for(let i = 0; i < 5; i++){
a ++
console.log(a)
}
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
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
# 三目操作符
var allowed = (age >= 18) ? 'yes': 'no' //三元操作符
console.log(allowed)
1
2
3
4
2
3
4
# switch case
option = '3'
switch(option){
case '1':
console.log(1);
break;
case '2':
console.log(2)
break;
default:
console.log('default')
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 集合
var obj = new Object();
var obj2 = {};
obj = {
name: 'zhangsan',
age: 20,
email: 'zhangsan@qq.com',
contact:{
phone: 123456,
area: '重庆市南岸区'
}
}
obj.contact.wechat = 'wx'
console.log(obj)
console.log(obj.name)
console.log(obj.contact)
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
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
# 数组
var arr = new Array()
var b = []
arr[0] = 'dog'
arr[5] = 'tiger' //中间是underfined
console.log(arr)
console.log(arr.length)
b = ['a', 'b', 'c']
console.log(b)
for(let i =0; i<b.length;i++){
console.log(b[i])
}
for (let i in arr){ //普通遍历会输出underfined,这种不会
console.log(arr[i])
}
arr.forEach(function(value){
console.log(value)
})
b.push('aaa') //末尾追加
console.log(b)
b.reverse() //反转
console.log(b)
b.shift() //删除第一个
console.log(b)
b.unshift('first') //数组第一个位置添加
console.log(b)
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
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
# 函数
a = 1
function add(x){ //只是定义了一下,还没执行
a += x;
}
add(2) //函数执行
console.log(a)
// function add(a, b, c){
// }
function add(){
let sum = 0
for (let i = 0, j = arguments.length; i < j ;i++){
sum += arguments[i]
}
return sum
}
let sum = add(1, 2, 3, 4, 5, 6, 7, 8)
console.log(sum)
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
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
# 闭包
//闭包(函数返回一个函数)
function makeAdder(a){
return function(b){
return a + b
}
}
var x = makeAdder(4)
sum = x(5)
console.log(sum)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
编辑 (opens new window)
上次更新: 2024/12/19, 09:15:30