函数基础
函数的基本写法
1
2function test(){}
表达式定义
1 | var test = function(){} |
函数的组成部分:函数名,[参数],[返回值],return
形参与实参
形参:参数占位符
1
2
3/* 获取形参的长度 */
function test(a,b,c){}
console.log(test.length); // 3实参:实际传递的参数
1
2
3
4
5
6
7
8
9
/* 获取实参的信息:arguments */
/* 获取实参的长度 */
function test(a,b){
console.log(arguments)
console.log(arguments.length);
}
test(1,2,3); // 3,可以看出实参和形参的个数不一定相等arguments反映了实参和形参的映射关系
1
2
3
4
5
6
7
8
9
10
11
12function test(a,b){
a = 3;
console.log(arguments[0]);
}
test(1,2); // 3
test(); // undefined
/* 总结:
* 1.形参被赋了值的,arguments对应的值可以修改
* 2.没有被赋值的形参,其对应的arguments值不可改,值为undefined
*/1
2
3
4
5
6
7
8
9
10
11
12function test(a,b){
arguments[0] = 3;
console.log(a);
}
test(1,2); // 3
test(); // undefined
/* 总结:
* 1.形参被赋了值的可以被arguments修改
* 2.没有被赋值的形参不可改,值为undefined
*/初始化参数
1
2
3
4
5
6function test(a = 1,b = 2){
console.log(arguments[0],a);
}
test(undefined,3); // undefined 1
/* 对形参初始化,a = 1
* 但是对应的实参值是undefined所以arguments[0]=undefined */1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/* 兼容性写法 */
function test(a,b){
a = arguments[0] || 1;
b = arguments[1] || 2;
}
// 或
function test(a,b){
if(typeof arguments[0] == 'undefined')
a = 1;
if(typeof arguments[1] == 'undefined')
b = 1;
}
//或
function test(a,b){
a = typeof arguments[0] == 'undefined'? 1 : a;
b = typeof arguments[1] == 'undefined'? 2 : b;
}
函数的返回
- return的作用:①终止函数执行 ②返回值,作为函数执行的结果
- 不指定return语句的函数,默认返回undefined
- 构造函数被实例化后返回this
函数基础总结
函数式编程:一个固定的功能或程序段被封装的过程,在这个封装体中需要一个入口(参数)和出口(返回)
函数的作用:把抽象重复的代码独立出来且有模块的单一责任制(希望一个功能出一个模块,并且希望这个模块不依赖其他模块,具有独立性),解耦合
函数进阶
变量提升和函数提升
1 | test(); // 函数正常执行 |
预编译
JS引擎:①通篇检查语法错误 ②预编译 ③ 解释一行执行一行
创建GO(Global Object)全局上下文
暗示全局变量(imply global variable)
1
2a = 1; // 未被声明就定义
console.log(a); // 11
2
3function test(){ var a = b = 1 }
test();
console.log(b); // 1在全局中不管是否声明(var)变量都为window的属性
代码示例
1 |
|

1 | function test(a){ |
上述代码AO创建的过程
所以①输出f a(){} ②输出1 ③输出1 ④输出f (){}
学习了AO,GO,就自然而然的引出了作用域与作用域链