-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.function.html
90 lines (68 loc) · 2.37 KB
/
20.function.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script>
//函式名稱
function aaa() {
console.log('hello');
}
aaa();
//匿名函式
//通常用在只執行一次,而且會立即執行(後面帶小括號)
(function () {
console.log('no name');
})();
//ES6 宣告函式的方式改變,用const宣告一個匿名函式名稱
const def = function () {
console.log('def');
};
def();
//funtion 後面的(),用處是什麼
//()裡面是用來傳遞參數使用的,不傳就空白
//加法的函式
function myfunc(a, b) {
console.log(a);
console.log(b);
console.log(a + b);
}
myfunc(123, 456);
myfunc(3323232, 443343);
//return 回傳,把一個計算後的結果傳回去
//所以我們必須要有一個變數去承接這個回傳結果
function returnAddResult(a, b) {
return (a + b);
}
const ans = returnAddResult(3, 4);
console.log(ans);
const ans2 = returnAddResult(22, 20);
console.log(ans2);
console.log(ans + ans2);
//ES6 箭頭函式,用() => 來表示 function 的簡稱
// jQuery不接受箭頭函式寫法
function normalFunc() {
console.log('normalfunc');
};
//等同寫法
const normalFunc = function () {
console.log('normalfunc');
}
const normalFunc = () => {
console.log('normalfunc');
}
normalFunc();
//箭頭函式的return如果只有一行,可省略
const myAdd = (a, b) => a + b;
console.log(myAdd(5, 6));
</script>
</body>
</html>