1.参数初始化及默认值
function test(a, b){console.log(a);console.log(b);}test(1); // a = 1; b = undefined// 直接给形参赋值,是ES6的写法function test(a, b = 1){console.log(a);console.log(b);}test(1); // 1 1test(1, undefined); // 1, 1
1.设置参数默认值的方式
1.1选择 || 方法
function test(a, b){var a = a || 1;var b = b || 2;var a = arguments[0] || 1;var b = arguments[1] || 2;console.log(a + b);}test(); // 3
2.typeof()
使用typeof(arguments[n])的方式,来检测其内部的参数是否为undefined,如果是undefined则代表没有传入参数,给它赋一个值
function test(a, b) {var a, b;if(typeof(a) === 'undefined'){a = 1;}else{a = arguments[0];}if(typeof(b) === 'undefined'){b = 2;}else{b = arguments[1];}console.log(a + b);}test(); //// 三元运算符function test(a, b) {var a = typeof(a) === 'undefined' ? 1 : arguments[0];var b = typeof(b) === 'undefined' ? 2 : arguments[1];console.log(a + b);}
2.递归
递归: 函数自己调用自己
使用规律:
1. 找出规律
- 设置递归出口
3.预编译
- 检查通篇语法错误
- 预编译的过程
- 解释一行执行一行 ```javascript test(); // 1 // 函数声明整体提升 function test() { console.log(1); } // 变量只有声明提升 console.log(a); // undefined var a = 10;
<a name="ufdXW"></a># 4.暗示全局变量 imply global variable```javascriptvar a = 1;b = 2;console.log(window.a); // a = window.a// b = window.b// window = {// a: 1,// b: 2// }// --------function test() {var a = b = 1;}test()console.log(window.a); // undefinedconsole.log(window.b); // 1
5.AO activation object 活跃对象 函数上下文
- 先寻找形参和变量声明
- 实参值赋值给形参
- 找函数声明,赋值
- 执行 ```javascript
- 案列1 function test(a) { console.log(a); // f a(){} var a = 1; console.log(a); // 1 function a(){} console.log(a); // 1 var b = function(){} console.log(b); // f b(){} function d(){} }
test(2);
AO = { a: undefined => 2 => function a(){}=> 1 b: undefined d: function d(){} }
```javascript2.案例2function 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 =>5b: undefined =>f b(){} =>6c: undefined =>0d: f d(){}}
5.GO global object 全局上下文
- 找变量
- 找函数声明
- 执行
- GO === window
```javascript 1.案例1 var b = 3; console.log(a); // function a(a){}console.log(a, b); // f a(){} undefined// 只有函数声明提升, 函数字面量(表达式)不会进行提升function a() {}var b = function (){}
function a(a) { console.log(a); // f a() {} var a = 2; console.log(a); // 2
function a() {} var b = 5; console.log(b); // 5 }
a(1);
GO = { b: undefined a: f a(a){} }
AO = { a: undefined => 1 => f a(){} => 2
b: undefined => 5 }
```javascripta = 1;function test() {console.log(a); // undefineda = 2;console.log(a); // 2var a = 3;console.log(a); // 3}test();var a;GO = {a: undefinedtest: f test() {}}AO = {a: undefined =>2 =>3}
function test() {console.log(b); // undefinedif(a) { // a => undefinedvar b = 2;}c = 3;console.log(c); // 3}var a;test();a = 1;console.log(a); // 1GO = {a: undefinedtest: f test() {}c: 3}AO = {b: undefined}
作业
1. 作业1function test() {return a; // f a() {}a = 1;function a() {}var a = 2;}console.log(test()); // f a() {}GO = {test: f test() {}}AO = {a: undefined =>f a() {}}
2. 作业2function test() {a = 1;function a() {}var a = 2;return a}console.log(test()); // 2GO = {test: f test() {}}AO = {a: undefined =>f a() {} =>1 =>2}
3. 作业3a = 1;function test(e) {function e() {}arguments[0] = 2;console.log(e); // 2if(a){ // undefinedvar b = 3;}var c;a = 4;var a;console.log(b); // undefinedf = 5;console.log(c); // undefinedconsole.log(a); // 4}var a;test(1);GO = {a: undefinedtest: f test() {}f: 5}AO = {e: undefined =>1 =>f e() {} =>2b: undefinedc: undefineda: undefined =>4}
