blog icon indicating copy to clipboard operation
blog copied to clipboard

JavaScript之this

Open sundjly opened this issue 5 years ago • 0 comments

源于之前的一次面试,请看题

const a = {
  count:1,
  b:{
    count: 2,
    getCount: function(){
      console.log(this.count);
    }
  }
}

const getb = a.b.getCount
console.log(getb())
console.log(a.b.getCount())

理解的话,首先要明确的是 this是在函数里面 来自这里 https://juejin.im/post/5c6bbf0f6fb9a049ba4224fd#heading-16 几个概念:堆栈,当前执行上下文(栈) (静态)作用域链 image

总结一句话,函数里面的this(非箭头函数): this 只有在运行时才会存在,且指向了当前执行上下文,在箭头函数里面:定义时绑定,this为参数,指向的是箭头函数上一层级作用域中那个this。

理解有误,请指正!

sundjly avatar Apr 04 '19 17:04 sundjly