一、形参赋值默认值
1.1、ES6形参赋值默认值
在传递实参值时,可以用undefined来进行占位
// 初始化参数-默认值// ES6形参赋值默认值function test(a = 1, b = 2){console.log(a, b);}test();// 在传递实参值的时候,可以用undefined来进行占位function func1(a = 1, b){console.log(a, b);}func1(undefined, 10)
1.2、ES5形参赋值默认值
利用实参集合和或运算的特点进行形参赋值默认值
// ES5形参赋值默认值// 利用实参集合和或运算的特点function func(a, b){var a = arguments[0] || 1;var b = arguments[1] || 2;return a + b;}console.log(func());
利用typeof的特点来进行形参赋值默认值
// ES5形参赋值默认值// 利用实参集合和typeof的特点以及三目运算符来实现形参赋值默认值function func(a, b){var a = typeof(arguments[0]) !== 'undefined'? arguments[0]: 1;var b = typeof(arguments[1]) !== 'undefined'? arguments[1]: 2;return a + b;}console.log(func());
二、作业「递归」
三、预编译流程
3.1、JS代码解析的过程
- 通篇检查语法错误
- 预编译
解释一行执行一行 ```javascript
// 预编译的流程// 1. 通篇检车语法错误// 2. 预编译的过程// 3. 解释一行执行一行// 函数声明整体会被提升test();function test(){console.log(1);}test();
// 变量只有声明会提升,赋值不会被提升console.log(a);var a = 10;console.log(b);var b;var c;console.log(c);// 1. 先声明变量a// 2. 再把值1赋值给变量avar a = 1;function a(){var a = 10;var a = function(){}}var a = 10;
<a name="aZ0bp"></a># 四、暗示全局变量> - 暗示全局变量:imply global variable> - 在全局作用域下,写不写var都是属于暗示全局变量> - 没有声明直接赋值的,属于暗示全局变量```javascript// 暗示全局变量:imply global variable// 在全局作用域下,写不写var都是属于暗示全局变量// 没有声明直接赋值的,属于暗示全局变量var a = 1;b = 2;console.log(a); // a = window.aconsole.log(window.b); // b = window.b// GO = {// a: 1,// b: 2// }
暗示全局变量:imply global variable,没有声明就直接赋值的
// 暗示全局变量:没有声明就直接赋值function func(){var a = b = 1;}func();console.log(b);console.log(window.b);console.log(typeof(a));console.log(a);console.log(window.a);
五、预编译的流程
5.1、函数作用域下的预编译流程
- 寻找形参和变量声明
- 统一形参和实参的值
- 寻找函数声明并赋值
- 执行
预编译练习
// 练习:预编译做过的事情,函数执行的时候就不用再去做了function func(a, b){console.log(a);c = 0;var c;a = 5;b = 6;console.log(b);function b(){}function d(){}console.log(b);console.log(a, c);}func(1);// 函数预编译流程分析// AO = {// a: undefined -> 1 -> 5// b: undefined -> function b(){} -> 6// c: undefined -> 0// d: function d(){}// }// 1// 6// 6// 5 0
5.2、全局作用域下的预编译流程
- 寻找变量声明
- 寻找函数声明并赋值
- 执行
GO === window
// 全局作用域下的预编译分析// 1. 寻找变量声明// 2. 寻找函数声明// 3. 执行// GO: global objectconsole.log(a);var a = 1;function a(){console.log(2);}console.log(a);// 全局作用域下的预编译分析// GO = {// a: undefined -> function a(){...}// }// function a(){...}// 1
// 练习console.log(a ,b);function a(){}var b = function(){}console.log(b);// 分析// GO = {// b: undefined// a: function(){}// }// function a(){} -> undefined// function(){}
// 练习console.log(a);var a = 1;function a(){console.log(2);}// 分析// GO = {// a: undefined -> function a(){...}// }// function a(){....}
// 练习var b = 1;function func(){var a = 1;var b = 2;console.log(a, b);}func();console.log(b);// 分析// GO = {// b: undefined -> 1// func: function func(){....}// }// AO = {// a: undefined -> 1// b: undefined -> 2// }// 1 2// 1
// 练习var b = 3;console.log(a);function a(a){console.log(a);var a = 2;console.log(a);function a(){}var b = 5;console.log(b);}a();// 分析// GO = {// b: undefined -> 3// a: function a(){....大函数}// }// AO = {// a: undefined -> function a(){....小函数} -> 2// b: 5// }// function a(){....大函数}// function a(){....小函数}// 2// 5
// 练习a = 1;function test(){console.log(a);a = 2;console.log(a);var a = 3;console.log(a);}test();var a;// 分析// GO = {// a: undefined -> 1// test: function test(){...}// }// AO = {// a: undefined -> 2 -> 3// }// undefined// 2// 3
5.3、条件判断下的变量提升
在条件判断中,不管条件是否成立都要进行变量提升;因为在预编译阶段,函数内部的代码是还没有执行的,所以是不看条件是否成立的
// 条件判断下的变量提升:不管条件是否成立,都要进行变量提升function test(){console.log(b);if(a){var b = 2;}c =3;console.log(c);}var a;test();// 分析// GO = {// a: undefined// test: function test(){...}// }// AO = {// b: undefined// }// undefined// 3
// 练习function test(){console.log(b);if(a){var b = 2;}c = 3;console.log(c);}var a;test();a = 1;console.log(a);// 分析// GO = {// a: undefined -> 1// test: function test(){....}// }// AO = {// b: undefined// }// undefined// 3// 1
六、作业
写出下面函数的AO
// 写出AOfunction test(){return a;a = 1;function a(){var a = 2;}}console.log(test());// 分析// AO = {// a: function a(){...}// }// a: function a(){...}
特别注意:函数不写return,那么默认的返回值是undefined
// 写出AOfunction test(){a = 1;function a(){var a = 2;return a;}}console.log(test());// 分析// AO1 = {// a: function a(){...} -> 1// }// AO2 = {// a: undefined -> 2// }// undefined
// 写出AOa = 1;function test(e){function e(){}arguments[0] = 2;console.log(e);if(a){var b = 3;}var c;a = 4;var a;console.log(b);f = 5;console.log(c);console.log(a);}var a;test(1);// 分析// GO = {// a: undefined -> 1// test: function test(){...}// }// AO = {// a: undefined - > 4// e: undefined -> 1 -> function e(){} -> 2// b: undefined// c: undefined// }// 2// undefined// undefined// 4
七、练习题
var a = false + 1;console.log(a);var b = false == 1;console.log(b);
八、面试题
if(typeof(a) && (-true) + (+undefined) + ''){console.log('pass');}else{console.log('no pass');}
// 输出结果是什么?console.log(!!" " + !!'' + !!false || 'pass');// 输出结果是什么?为什么?window.a || (window.a = '1');console.log(a);
