• Home
  • About
    • 知易行难 photo

      知易行难

      front-end developer

    • Learn More
    • Email
    • Github
    • Weibo
  • Posts
    • All Posts
    • All Tags
  • Projects

JavaScript中this的优先级

17 Apr 2017

Reading time ~1 minute

如果要判断一个运行中函数的this绑定,就需要找到这个函数的直接调用位置。找到之后就可以顺序应用下面这四条规则来判断this的绑定对象。

  1. 由new 调用?绑定到新创建的对象。
  2. 由call 或者 apply (或者bind)调用?绑定到指定的对象。
  3. 由上下文对象调用?绑定到那个上下文对象。
  4. 默认: 在严格模式下绑定到undefined,否则绑定到全局对象。


javascriptthis Share Tweet +1