H246802 / 30-days-challenge

30天每日打卡
4 stars 0 forks source link

day-05-函数不同执行次数,打印不同 #5

Open H246802 opened 5 years ago

H246802 commented 5 years ago

写一个函数 fn,使得 fn满足一下条件

H246802 commented 5 years ago

第一种 直接函数通过return 解决

function fn(){
     console.log('a')
     return function(){
    console.log('b')
        return function(){
        console.log('c')
        }
    }
}
H246802 commented 5 years ago

第二种 通过异步代码实现

function fn(){
    var x = 'a'
    var time = setTimeout(()=> console.log(x),0)
    return function(){
        x = 'b'
        return function(){
            clearTimeout(time)
            console.log('c')
        }
    }
}