记录_面试题_笔试篇

111 阅读3分钟

常见异步笔试题

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');


/* 输出结果:
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeout
*/

2. 第1题的变式一

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

async function async2() {
  // 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')

/* 输出结果:
script start
async1 start
promise1
promise3
script end
promise2
async1 end
promise4
setTimeout
*/

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')

/* 输出结果:
script start
async1 start
promise1
script end
promise2
setTimeout3
setTimeout2
setTimeout1
*/

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')

/* 输出结果:
script start
a1 start
a2
promise2
script end
promise1
a1 end
promise2.then
promise3
setTimeout
*/

常见闭包笔试题

1. 请写出输出内容

for (let i = 0; i < 4; i++) {
  setTimeout(function () {
    console.log(i)
  }, 300)
}

/* 输出结果:
4
4
4
4
*/

// 可能部分人会认为打印的是 0,1,2,3? 其实不是的, 因为 setTimeout 是异步的, 所以会先执行完 for 循环, 然后再执行 setTimeout, 所以打印出来的都是 4.


// 修改使其正常打印 (使用闭包使其正常打印)
for (let i = 0; i < 4; i++) {
  setTimeout(
    (function () {
      let temp = i
      return function () {
        console.log(temp)
      }
    })(),
    300
  )
}

/* 输出结果:
0
1
2
3
*/

2. 请写出输出内容

var a = 0
function foo() {
  var b = 14
  function fo() {
    console.log(a, b)
  }
  fo()
}
foo()

/* 输出结果:
0 14
*/

3. return 回一个函数

var n = 10
function fn() {
  var n = 20
  function f() {
    n++
    console.log(n)
  }
  return f
}

var x = fn()
x()

/* 输出结果:
21
*/

4. 函数作为参数

var a = '测试'
function foo() {
  var a = 'foo'
  function fo() {
    console.log(a)
  }
  return fo
}

function f(p) {
  var a = 'f'
  p()
}
f(foo())

/* 输出结果:
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)
}

/* 输出结果:
0
1
2
3
4
*/

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)

/* 输出结果:
undefined
undefined
测试
*/

2. 请写出输出内容

console.log(a)
var a = '测试'
function fn() {
  console.log(a)
  a = 12
}
fn()
console.log(a)

/* 输出结果:
undefined
测试
12
*/

3. 请写出输出内容

console.log(a)
a = '测试'
function fn() {
  console.log(a)
  a = 12
}
fn()
console.log(a)

/* 输出结果:
ReferenceError: a is not defined
*/

4. 请写出输出内容

var a = '测试'
function fn() {
  if (!a) {
    var a = 12
  }
  console.log(a)
}
fn()

/* 输出结果:
12
*/

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)

/* 输出结果:
12
13
0
*/