常见异步笔试题
1. 请写出输出内容
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2');
}
console.log('script start');
setTimeout(function() {
console.log('setTimeout');
}, 0)
async1();
new Promise(function(resolve) {
console.log('promise1');
resolve();
}).then(function() {
console.log('promise2');
});
console.log('script end');
2. 第1题的变式一
async function async1() {
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2() {
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout')
}, 0)
async1()
new Promise(function (resolve) {
console.log('promise3')
resolve()
}).then(function () {
console.log('promise4')
})
console.log('script end')
3. 第1题的变式二
async function async1() {
console.log('async1 start')
await async2()
setTimeout(function () {
console.log('setTimeout1')
}, 0)
}
async function async2() {
setTimeout(function () {
console.log('setTimeout2')
}, 0)
}
console.log('script start')
setTimeout(function () {
console.log('setTimeout3')
}, 0)
async1()
new Promise(function (resolve) {
console.log('promise1')
resolve()
}).then(function () {
console.log('promise2')
})
console.log('script end')
4. 第1题的变式三
async function a1() {
console.log('a1 start')
await a2()
console.log('a1 end')
}
async function a2() {
console.log('a2')
}
console.log('script start')
setTimeout(() => {
console.log('setTimeout')
}, 0)
Promise.resolve().then(() => {
console.log('promise1')
})
a1()
let promise2 = new Promise(resolve => {
resolve('promise2.then')
console.log('promise2')
})
promise2.then(res => {
console.log(res)
Promise.resolve().then(() => {
console.log('promise3')
})
})
console.log('script end')
常见闭包笔试题
1. 请写出输出内容
for (let i = 0; i < 4; i++) {
setTimeout(function () {
console.log(i)
}, 300)
}
for (let i = 0; i < 4; i++) {
setTimeout(
(function () {
let temp = i
return function () {
console.log(temp)
}
})(),
300
)
}
2. 请写出输出内容
var a = 0
function foo() {
var b = 14
function fo() {
console.log(a, b)
}
fo()
}
foo()
3. return 回一个函数
var n = 10
function fn() {
var n = 20
function f() {
n++
console.log(n)
}
return f
}
var x = fn()
x()
4. 函数作为参数
var a = '测试'
function foo() {
var a = 'foo'
function fo() {
console.log(a)
}
return fo
}
function f(p) {
var a = 'f'
p()
}
f(foo())
5. 自执行函数
var n = '测试'
;(function p() {
console.log(n)
})()
6. 循环赋值
for (var i = 0; i < 5; i++) {
;(function (j) {
setTimeout(function () {
console.log(j)
}, 1000)
})(i)
}
7. 节流防抖
function throttle(fn, timeout) {
let timer = null
return function (...arg) {
if (timer) return
timer = setTimeout(() => {
fn.apply(this, arg)
timer = null
}, timeout)
}
}
function debounce(fn, timeout) {
let timer = null
return function (...arg) {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arg)
}, timeout)
}
}
常见变量提升笔试题
1. 请写出输出内容
console.log(a)
var a = '测试'
function fn() {
console.log(a)
var a = 12
}
fn()
console.log(a)
2. 请写出输出内容
console.log(a)
var a = '测试'
function fn() {
console.log(a)
a = 12
}
fn()
console.log(a)
3. 请写出输出内容
console.log(a)
a = '测试'
function fn() {
console.log(a)
a = 12
}
fn()
console.log(a)
4. 请写出输出内容
var a = '测试'
function fn() {
if (!a) {
var a = 12
}
console.log(a)
}
fn()
5. 请写出输出内容
var a = 12,
b = 13,
c = 14
function fn(a) {
a = 0
var b = 0
c = 0
}
fn(a)
console.log(a)
console.log(b)
console.log(c)