一、默认参数值
1.形参默认值
1. 一般情况下,没有实参,形参默认值为undefind
2. 可以在定义函数的时候设置默认值,不过是ES6的写法
function test(a = 1, b) {
console.log(a); // 1
console.log(b); // undefined
}
test();
//第二种
function test(a = 1, b) {
console.log(a); // 2
console.log(b); // 1
}
test(2,1);
2. 设置默认值
1.利用arguments || 运算实现
2.判断typeof(arguments[])是否为undefind,注意typeof()返回的是字符串
function test1(a, b) {
var a = arguments[0] || 2;
var b = arguments[1] || 3
console.log(a + b);
}
test(); // 5
function test2(a, b) {
var a = typeof(arguments[0]) === 'undefined' ? 2 : arguments[0];
var b = typeof(arguments[1]) === 'undefined' ? 3 : arguments[1];
console.log(a + b);
}
test2(); // 5
二、递归
找规律
給一个出口
三、预编译
1. 步骤
通篇检查语法错误
预编译过程
解释一行,执行一行
2. 变量提升
函数声明整体提升,变量只有声明提升,赋值不提升
test();
function test(){
var a = 1;
console.log(a);
}
//a = 1;
console.log(a);
var a; // a = undefind
var a = 10; //a = undefind
3. AO active object 活跃对象 函数上下文
1. 寻找变量和变量声明
2. 实参赋值给形参
3. 找函数声明,赋值
4. 执行
function test(a){
console.log(a);
var a = 1;
con