javascript this关键字如何绑定_哪些规则决定了它的值?

普通函数直接调用时,非严格模式下this指向全局对象(如window),严格模式下为undefined;箭头函数无this,继承外层作用域;call/apply/bind可手动指定this,bind返回的新函数this不可再被call/apply更改;对象方法赋值后调用会丢失this绑定;new调用时this指向新创建的实例。

普通函数调用时 this 指向什么

直接调用 fn(),非严格模式下 this 指向全局对象(浏览器中是 window),严格模式下是 undefined。这是最容易出错的场景——你以为在对象方法里写了个函数,结果忘了绑定,this 就飞了。

  • 没加 use strict 时,function foo() { console.log(this); } foo(); 输出 window
  • 加了 use strict,同样调用会报错或输出 undefined(取决于上下文)
  • 箭头函数不遵循这条规则——它没有自己的 this,而是继承外层作用域的 this

call / apply / bind 如何强行指定 this

这三个方法本质都是手动传入第一个参数作为函数执行时的 this 值,区别只在参数传递方式:call 用逗号分隔,apply 用数组,bind 返回新函数且可预设部分参数。

function greet(greeting, punctuation) {
  return `${greeting}, ${this.name}${punctuation}`;
}
const person = { name: 'Alice' };

greet.call(person, 'Hello', '!');     // "Hello, Alice!"
greet.apply(person, ['Hi', '?']);     // "Hi, Alice?"
const hiAlice = greet.bind(person, 'Hi');
hiAlice('!');                         // "Hi, Alice!"
  • bind 返回的函数无法再被 callapply 改变 this(除非用 new 调用)
  • 如果 bind 的第一个参数是 nullundefined,非严格模式下仍会被转为全局对象

对象方法中的 this 为什么有时失效

当把对象方法赋值给变量或作为回调传入时,this 会丢失绑定,变成默认绑定规则(上一条)。常见于事件监听、定时器、Promise 回调等场景。

  • const btn = document.getElementById('btn'); btn.onclick = obj.handleClick;this 指向 btn,不是 obj
  • setTimeout(obj.method, 100);this 是全局或 undefined
  • 修复方式:用 obj.method.bind(obj)() => obj.method()、或在类中用属性箭头函数(method = () => {...}

new 调用时 this 怎么确定

new 调用函数时,引擎会创建一个新对象,把 this 绑定到该对象,并让其原型指向构造函数的 prototype。此时无论函数内部怎么写 this,都指向这个新实例。

function Person(name) {
  this.name = name;        // 这里的 this 就是 new 出来的实例
  this.say = function() {
    return `I'm ${this.name}`; // this 依然指向实例
  };
}
const p = new Person('Bob');
p.say(); // "I'm Bob"
  • 如果构造函数显式返回一个对象,则 this 绑定的新实例被忽略,返回值替代它
  • 箭头函数不能用 new 调用,会直接抛错 TypeError: xxx is not a constructor
  • ES6 class 内部方法默认不绑定 this,和普通函数一致;class 构造器中 this 才是新实例
实际写代码时,最常踩的坑不是记不住规则,而是混淆「定义时的作用域」和「调用时的绑定」。尤其是把方法传给第三方库(比如 React 的 onClick、Lodash 的 debounce)时,得时刻问一句:这个函数被谁调用?this 是谁?