1 参数默认值
1.1 初始化参数
- 默认值:undefined
形参可以直接给值
//第一种情况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);}test(1,2);//1 , 2/*总结:谁不是undefined,找谁*/
不支持es6的浏览器无法使用直接给形参设置默认值,解决方法:
function test(a,b){var a = agruments[0] || 1;//传入是0怎么办?var b = arguments[1] || 2;console.log(a);console.log(b);//或者var a,b;if(typeof(arguments[0]) !== 'undefined'){a = arguments[0];}else{a = 1;}console.log(a + b);//或者 三元运算符 推荐!var a = typeof(argument[0] !== 'undefined' ? arguments[0] : 1);}
2.递归
2.1 原则:
慎用
-
2.2 重要因素
找到计算规律
-
3.预编译
3.1 过程
检查通篇的语法错误
- 预编译的过程
-
3.2 变量提升 ⭐
总结:函数声明整体提升,变量只有声明提升,赋值不提升
基础案例:
//1.test(); //1 仍然可以打印function test(){console.log(1);}//2.console.log(a);//undefined 不会报错,但是打印不出10var a = 10;/*总结:函数声明整体提升,变量只有声明提升,赋值不提升*///3console.log(a);//打印函数体function a(a){var a = 10;var a = function(){}}var a = 1;
函数上下文 AO activation object
/*1.寻找函数的形参和变量声明;2.把实参的值赋给形参3.寻找函数的声明,赋值函数体4.执行函数*///1.function test(a){console.log(a);var a = 1;console.log(a);function a(){}console.log(a);var b = function(){}console.log(b);function d(){}}/*AO activation object活跃对象,函数上下文1.寻找函数的形参和变量声明AO = {a: undefined,b: undefined,}2.把实参的值赋值给形参AO = {a: undefined -> 2,b: undefined,}3.寻找函数的声明,赋值函数体AO = {a: undefined -> 2 -> function a(){} ,b: undefined,d: function d(){}}4.执行函数AO = {a: undefined-> 2-> function a(){}-> 1,b: undefined -> function(){},d: function d(){}}*///2.function test(a, b){console.log(a); //1c = 0;var c;a = 5;b = 6;console.log(b); //6function b(){}function d(){}console.log(b); //6}test(1)/*AO = {a : undefined-> 1;b : undefined-> function b(){};c : undefined;d : function d(){};}*/
全局上下文
/*1.寻找变量声明2.寻找函数声明,赋值函数体3.执行函数*///1.var a = 1;function a(){console.log(2);}console.log(a);//1//2.function test(){var a = b = 1;console.log(a);}test();/*GO = {b = 1;}AO = {a : undefine d -> 1;}*///3.var b = 3;console.log(a);//function(){}function a(a){console.log(a);//function(){}var a = 2;console.log(a);.//2function a(){var b = 5;console.log(b);//5}}a(1);/*GO = {b = 3;a = function(){}}AO = {a = undefined-> 1-> function(){};AO2 = {b = undefind-> 5;}}*///4.a = 1;function test(){console.log(a);//undefineda = 2;console.log(a);//2var a = 3;console.log(a);//3}/*GO = {a = 1;test = function();}AO = {a = undefined-> 2-> 3;}*///5.function test(){console.log(b);//undefined;if(a){var b = 2;}c = 3;console.log(c);//3}var a;test();a = 1;console.log(a);//1/*GO = {a = undefined;test = function(){}c = 3;}AO = {AO2 = {b = undefined; ⭐}}*/
3.3 暗示全局变量 imply global variable ❓
案例 ```javascript //1 a = 1; var b = 2; console.log(a);//1 console.log(b);//2 / 以上声明方式都将a和b放到 window = { a = 1, b = 2; } / //2. function test(){ var a = b = 1; } console.log(b); //1 为什么?为什么会提升到全局作用域
<a name="dIJvX"></a>### 3.4 作业 ❓```javascript/* 第一题 */function test(){return a; //functiona = 1;function a(){}var a = 2;}console.log(test());//function/*GO = {test = function test(){};a = 1;}AO = {a = undefined-> function(){}-> 2;}*//*第二题*/function test(){a = 1;function a(){}var a = 2;return a;}console.log(test());//2/*GO = {a = 1;}AO = {a = undefined-> function(){}-> 2;}*//* 第三题 */a = 1;function test(e){function e(){}arguments[0] = 2;console.log(e); //2if(a){var b = 3;}var c;a = 4;var a;console.log(b);//undefinedf = 5;console.log(c);//undefined;console.log(a);//4}var a;test(1);/*GO = {a = undefined-> 1;test = test(e){};f = 5;}AO = {e = undefined-> 1-> function(){}-> 2;b = undefined;c = undefined;a = undefined-> 4;}*///4if(typeof(a) && (-true) + (+undefined) + ''){console.log("通过了");}else{console.log("没通过");}//5if(1 + 5 * '3' == 16){console.log("通过了");}else{console.log("没通过");}//6console.log(!!' ' + !!'' - !!false || '通过了');
