JS基础学习(4)

62 阅读3分钟

递归、预编译初见

  • 函数参数默认值

function test(a,b){
    // 映射关系
    var a = arguments[0] || 1;
    var b = arguments[1] || 2;
    
    // 也可以用 typeof()
    // typeof打印出来的都是字符串
    var a,b;
    if(typeof(argumets[0]) !== 'undefined'){
        a = arguments[0];
    }else {
        a = 1;
    }
    
    if(typeof(argumets[1]) !== 'undefined'){
        b = arguments[1];
    }else {
        b = 2;
    }
    
    return a + b;
}
// 任然可以传入实参
test(12,13);

  • n的阶乘 -- 不能用循环
  • 递归要找规律,要找出口
var n = 5;
    // n! = n * (n -1)!
    /*
        test1(5) = 5 * test1(4)
        test1(4) = 4 * test1(3)
        test1(3) = 3 * test1(2)
        test1(2) = 2 * test1(1)
    */
    var test = function test1(n){
        if(n === 1) return 1;
        return n * test1(n -1);
    }
  • 斐波那契数列
  • 找规律 n3 = n2 + n1
  • 出口 n <= 2
 function test(n){
        if(n <= 2) return 1;
        return test(n -1) + test(n -2);
    }
 console.log(test(6));
  • 递归总是走到出口的时候在一步一步返回结果,一定要找到一个出口结束掉递归。

预编译前奏

  • JS首先通篇检查语法错误
  • 中间有个,预编译的过程
  • 解释一行,执行一行
  • 函数声明整体提升,var变量只有声明,赋值是不提升的
    • var a = 1; 是两个步骤 1. 声明a ,2. 把 1 赋值给变量 a

全局变量问题 imply global

    var a = 10;
    console.log(a);// 没问题
    
    b = 20;
    console.log(b)// 也没问题
    
    // 全局下声明,都是存储在全局的 window对象上的
    // a = window.a;
    // b = window.b;
    
    // 连等
    // 这个b并没有在函数内声明
    function test(){
        var a = b = 1;
    }
    test();
    console.log(window.b);// 可以 提升到window ,a没有
    

预编译问题

  • 函数预编译就是在函数执行之前进行的一个步骤
  • AO activation object 活跃对象(函数上下文)
  • 预编译做过的,函数执行就不在做了

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(){};
}
test(2);

/*
    首先创建AO对象
    1. 寻找函数里面的形参和变量声明
    AO = {
        a:undefined,
        b:undefined
    }
    2. 把实参的参数值赋值给形参
    AO = {
        a:2,
        b:undefined
    }
    3. 寻找函数的声明赋值函数体
    AO = {
        a:function a(){},
        b:undefined,
        d:function d(){}
    }
    4. 执行函数 在哪里打印的就看那一步
     ƒ a(){}
     1
     1
     ƒ (){}
*/

function test(a,b){
    console.log(a);
    c = 0;
    var c;
    a = 5;
    b = 6;
    console.log(b);
    function b(){};
    function d(){};
    console.log(b);
}
test(1);

/*
    1. 函数内形参和变量声明
    AO = {
        a:undefind,
        b:undefind,
        c:undefind
    }
    
    2. 把实参的参数赋值给形参
    AO = {
        a:1,
        b:undefind,
        c:undefind
    }
    
    3. 寻找函数的声明赋值函数体
    AO = {
        a:1,
        b:function b(){},
        c:undefind,
        d:function d(){}
    }
    
    4. 执行函数
    1  6 6
*/

  • 全局上下文 global object -- GO
  • GO === window
var a = 1;
function a(){};
console.log(a);// 1

/*
    1. 寻找变量 声明!! 声明!!声明!! 
    GO={
        a:undefined
    }
    2. 寻找函数声明
    GO={
        a:function(){}
    }
    3. 执行
    1
*/

    console.log(a,b);// 
    function a(){};
    // 匿名函数是表达式 不会提升
    var b = function(){};


var b = 3;
console.log(a);// hanshu
function a(a){
    console.log(a); // function a(){}
    var a = 2;
    console.log(a);// 2
    function a(){
        var b = 5;
        console.log(b);
    }
}
a(1);

/*
   ƒ a(a){
    console.log(a); // function a(){}
    var a = 2;
    console.log(a);// 2
    function a(){
        var b = 5;
        console.log(b);
    }
}
 ƒ a(){
        var b = 5;
        console.log(b);
    }
 2
*/


function test(){
    return a;
    a = 1;
    function a(){};
    var a = 2;
}

console.log(test());

/*
    1. 函数内变量
    AO ={
        a:undefind,
    }
    
    2. 寻找函数的声明赋值函数体
    AO={
        a:function a(){}
    }
    
    3. 执行 
    遇到return a 返回 终止函数
    function a(){}
*/