关于this、宏任务、微任务的一道面试题

145 阅读1分钟
var a = 1
let obj = {
  a:2,
  b1: function(){
    console.log(this.a, this) // 2 obj
  },
  b: () => { // 所在的词法作用域是全局环境?
    console.log(this.a, this) // 在浏览器中是1 window;在node中是 undefined {};
  },
  c: function(){
    console.log(a) // undefined
    var a = 3
  },
  d: function(){
    console.log(a) // a is not defined referrence error
    let a = 3 // 导致暂时性死区
  },
  e: function () { // 0 2 4 1
    console.log(0)

    setTimeout(_ => console.log(1))

    new Promise(resolve => {
      console.log(2)
    }).then(() => {
      // 挖坑啊,上面没有resolve
      console.log(3)
    })

    console.log(4)
  }
}