SOURCE

console 命令行工具 X clear

                    
>
console
/*
闭包: 函数㠌套函数,内部函数就是闭包
正常情况下,函数执行完成,内部变量会销毁(即释放内存空间)
闭包:内部函数没有执行完成,外部函数 变量不会被销毁

应用: 封装一段代码
*/

/*
// 函数作为返回值
function test(){
    //const a=1;
    return function(){
        console.log(a)
    }
}
const a = 2;
const fn = test()
fn()

// 函数作为参数
function test1(fn) {
    const c = 1;
    fn1();
}
const c = 2;
function fn1(){
    console.log(c)
}

test1(fn);




// 对象拷贝 深拷贝和浅拷贝


//浅拷贝
let obj = {
    name:"小明",
    age:20
}

function copy(obj){
    let newObj = {};
    for(let i in obj){
        newObj[i]=obj[i];
    }  
    return newObj;
}


//递归
// 递归就是自已调自已
function fun(n){
    if(n===1){
        return 1;
    }else{
        return n + fun(n-1);
    }
}
let result = fun(3);
console.log(result);   //结果等于6



//深拷贝
let obj = {
    name:"小明",
    age:20,
    friend:{            
        name:"小红"
    }       //因为这里又是一个对象,之前的方法无法拷贝到,所以还是一个引用类型,下面通过递归实现深拷贝
}

function copy(obj){
    let newObj = {};
    for(let i in obj){
        if(obj[i] instanceof Objetct){
            newObj[i] = copy(obj[i]);
        }else{
            newObj[i]=obj[i];
        }    
    }  
    return newObj;
}


//深拷贝的另一种方法
function copy(obj){
    let str = JSON.stringify(obj);
    let newObj = JSON.parse(str);
    return newObj
}
// 将obj对象转换为字串,再将字串转为json
const student2 = copy(studen1);
console.log(student1);
console.log(student2);
*/