参数默认值
- 参数 ```javascript function test(a=1,b=2){ console.log(a,b)//a=1,b=1; } test(undefined,1)
- **形参 和 实参谁的值不是undefined,就取谁的;**```javascriptfunction test(a=undefined,b=2){console.log(a,b)//a=1,b=2}test(1,undefined);
新版本和老版本服务器兼容问题
function test(a,b){var a = arguments[0]||1;var b = arguments[0]||2;//或者var a = typeof(a)!=='undefined'?a:1;var b = typeof(b)!=='undefined'?b:2;console.log(a,b);}
递归
找到规律
找到出口
//n的阶乘function nMul (n){if(n==1){return 1}return n*nMul(n-1);}nMul(5)//120
预编译
过程
检查通篇语法错误 如果有、整个代码都不执行
- 预编译
-
变量提升
函数声明整体提升,变量只有声明提升,赋值不提升。
test();function test(){console.log(1) //1}console.log(a);//undefined变量提升var a = 10;
暗示全局变量 Imply global variable
没有声明就赋值变量
- 全局不声明,直接挂在到window
a=10;// 等同于 window.a;function test(){var a = b = 1;//1:var a->全局声明b -> b=1 ->a = b;}test();console.log(b) //b=1;console.log(window.b)//b=1;console.log(a)//a is not definedconsole.log(window.a) //undefined
AO 活跃对象 (activation object)
- 找到形参和变量声明
- 形参和实参相对应
- 找到函数声明,赋值。
- 执行 ```javascript function test(a){ console.log(a);//function(){}; var a =21; console.log(a);//21 function a(){}; console.log(a);//21 var a = function(){console.log(‘我是一个函数’)} console.log(a);// function(){console.log(‘我是一个函数’)} } test(123) /**
- 找到变量声明
- 找到函数声明,赋值
- 执行。
例子:
AO 例子1
function test(a, b) {console.log(a);//5 undefinedc = 0;var c;a = 5;b = 6;console.log(b);//6function b() { }function d() { }console.log(b);//6}test();/*** AO :{* a:undefined->,* b:undefined->function b(){}->6* c:undefined* d:function d(){}* }*/
