JS编程题

51 阅读1分钟

输出

1.考察闭包、作用域链

function fun(n, o) {
    console.log(o)
    return {
        n:10,
        fun: function(m) {
            return fun(m, n);
        }
    };
}
let a = fun(0);  //undefined
a.fun(1);        //fun(1,0) => 0
a.fun(2);        //fun(2,0) =>0
a.fun(3);       //fun(3,0) => 0
var b = fun(0).fun(1).fun(2).fun(3);
//undefined 
//fun(1,0) => 0
//fun(2,1) => 1
//fun(3,2) => 2
                
                
var c = fun(0).fun(1);
//undefined
//fun(1,0) => 0
               
c.fun(2);  //fun(2,1) => 1     
c.fun(3);  //fun(3,1) => 1     

2.考察事件循环、promise、async/await

async function async1() {
    console.log('async1 start')
    await async2()
    console.log('async1 end')
}

async function async2() {
    console.log('async2')
}

console.log('script start')

setTimeout(() => {
    console.log('setTimeout')
}, 0)

async1()

new Promise(resolve => {
    console.log('promise1')
    resolve()
}).then(() => {
    console.log('promise2')
})
// script start 
// async1 start
// async2
// promise1
// async1 end
// promise2
// setTimeout