函数基础
- 耦合
- 高内聚,低耦合 -> 模块的单一责任制
-
函数种类
最基本的函数写法 - 函数声明
function test(参数){函数的执行语句;}// 被调用才会执行test();
匿名函数表达式 函数字面量
var test = function(){ -> 匿名函数函数的执行语句;}
形实参及映射
形参 -> 占位 实参
- 形参和实参数量可不等
- arguments 实参
- arguments.length // 实参个数 函数名.length // 形参个数 ```javascript function test(a, b) { a = 3; console.log(arguments[0]); // 3 函数可以改变参数 } test(1,2);
function test(a, b) { b = 3; console.log(arguments[1]); // undefined 实参未被定义 } test(1);
// 总结:给形参赋值的,可以改变形参,未给形参赋值的,改变形参没用
function test(a, b) { a = 3; // a 存在栈内存中 console.log(arguments[0]); // arguments 存在堆内存中 所以a和arguments[0]不是同一个值,映射关系 } test(1,2);
- return 作用:1.终止函数执行 2.返回相应的值```javascriptfunction test() {console.log('执行完成');// return; 没写JS引擎隐式添加return}test();
变量类型
- 全局变量 局部变量 [[scope]]
- 函数: 一个固定的功能或者是程序段被封装的过程,实现一个固定的功能或是程序,在这个封装体中需要一个入口和一个出口,入口就是参数,出口就是返回
函数参数默认值
- 初始化参数 默认值:undefined
- es6 ```javascript function test(a = 1, b) { console.log(a); console.log(b); }
test(undefined, 2); // 1,2
function test(a = undefined, b) { console.log(a); console.log(b); // a 1 // arguments[0] }
test(1, 2); // 1,2
//兼容写法 function test(a,b){ // var a = arguments[0] || 1; // var b = arguments[1] || 2; var a = typeof(arguments[0]) !== ‘undefined’ ? arguments[0] : 1; var b = typeof(arguments[1]) !== ‘undefined’ ? arguments[1] : 2; }
<a name="vw6Dg"></a>## 递归```javascript// n的阶乘 不使用for循环 递归// fact(n)// n! = n * (n-1)! 规律// 出口function fact(n){if(n === 1) {return 1;}return n * fact(n-1);}递归:找到规律找到出口
预编译
- // 1. 检查通篇的语法错误
- // 1.5 预编译的过程
- // 2. 解释一行,执行一行 ```javascript test(); function test(){ console.log(1); }
console.log(a); // undefined var a = 10;
函数声明整体提升 变量只有声明提升,赋值不提升
var a = 1; 2个步骤: 声明变量a, 赋值
<a name="xl3Cz"></a>## 暗示全局变量```javascriptvar a = 1;b = 2;window = {a : 1,b : 2}function test(){var a = b = 1;}test();console.log(a); //报错window.b // 1
function test(a) {console.log(a); // function a(){}var a = 1;console.log(a); // 1function a(){};console.log(a); // 1var b = function(){}console.log(b); // function(){}function d(){}}test(2);// AO activation object 活跃对象 函数上下文// 1 寻找形参和变量声明// 2 实参值赋值给形参// 3 找函数声明,赋值// 4 执行AO = {a: undefined -> 2 -> function a(){} -> 1b: undefined, -> function(){}d: function d(){}}
重点:
// AO activation object 活跃对象 函数上下文
// 1 寻找形参和变量声明
// 2 实参值赋值给形参
// 3 找函数声明,赋值
// 4 执行
// GO global object 全局上下文
// 1 找变量
// 2 找函数声明
// 3 执行
var a = 1;function a() {console.log(2);}console.log(a); // 1// GO global object 全局上下文// 1 找变量// 2 找函数声明// 3 执行GO = {a: undefined -> function a(){} -> 1;}// GO === windowconsole.log(a, b); // function a(){} undefinedfunction a(){}var b = function(){}GO = {b: undefined,a: function a(){}}
function test(){var a = b = 1;console.log(a); // 1}test();GO = {b: 1}AO = {a: undefined -> 1}
var b = 3;console.log(a); // function a(a){...}function a(a){console.log(a); // function a(){}var a = 2;console.log(a); // 2function a(){}var b = 5;console.log(b); // 5}a(1);GO = {b: undefineda: function a(){...}}AO = {a: undefined1function a(){}2b: undefined,5}
a = 1;function test(){console.log(a); // undefineda = 2;console.log(a); // 2var a = 3;console.log(3); // 3}test();var a;GO = {a: undefined,1test: funtion test(){...}}AO = {a: undefined23}
funtion test(){console.log(b); // undefinedif(a){var b = 2;}c = 3;console.log(c); // 3}var a;test();a = 1;console.log(a); // 1GO = {a: undefined,1test: funtion test(){...},c: 3}AO = {b: undefined}
题目:
function a(){}
2
