-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_function_object.html
More file actions
106 lines (84 loc) · 3.43 KB
/
js_function_object.html
File metadata and controls
106 lines (84 loc) · 3.43 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!--
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-10-15 12:18:26
* @LastEditTime: 2020-10-18 15:04:50
* @FilePath: \web\javascript\js_function_object\js_function_object.html
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
-->
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title> function 对象</title>
<script>
/*
Function:函数(方法)对象
1. 创建:
1. var fun = new Function(形式参数列表,方法体); //忘掉吧
2. function 方法名称(形式参数列表){
方法体
}
3. var 方法名 = function(形式参数列表){
方法体
}
2. 方法:
3. 属性:
length:代表形参的个数
4. 特点:
1. 方法定义是,形参的类型不用写,返回值类型也不写。
2. 方法是一个对象,如果定义名称相同的方法,会覆盖
3. 在JS中,方法的调用只与方法的名称有关,和参数列表无关
5. 调用:
方法名称(实际参数列表);
*/
//1.创建方式1
var fun1 = new Function("a", "b", "alert(a);");
//调用方法
fun1(13,33);
document.write("fun1.length = " + fun1.length + "<br>");
document.write("<hr>");
//2. 创建方式2
function fun2(a, b){
var sum = a + b;
document.write("sum = " + sum + "<br>");
}
fun2(1, 10);
document.write("<hr>");
var fun3 = function(a, b){
document.write("a + b = " + (a + b) + "<br>");
}
document.write("fun3.length = " + fun3.length + "<br>");
fun3(3,3);
document.write("<hr>");
function fun4(a , b){
document.write("a = " + a + "<br>");
document.write("b = " + b + "<br>");
}
//方法调用
fun4(3,4);
fun4(1);
fun4();
fun4(1,2,3);
document.write("<hr>");
/**
* 求任意个数的和
*/
function add() {
var sum = 0;
// 在方法声明中有一个隐藏的内置对象(数组),arguments,封装所有的实际参数
for(var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
var sum = add(1, 2, 3, 4);
document.write("sum = " + sum + "<br>");
</script>
</head>
<body>
</body>
</html>