Skip to main content

12 posts tagged with "JavaScript"

View All Tags
· 3 min read

this 指向在函数定义时不确定,调用时才确定。

  1. 全局环境:浏览器 this === window,Node 中 this === global,严格模式为 undefined
  2. 对象方法obj.method() 中 this 指向点号左侧的对象
  3. 独立调用:函数脱离对象后丢失绑定,this 回退到全局(严格模式 undefined)
  4. 箭头函数:没有独立的 this,捕获定义时所在作用域的 this
  5. 显式绑定call/apply/bind 可手动指定任意 this

核心原则:只看如何调用,不看在哪定义。