普通函数直接调用时,非严格模式下this指向全局对象(如window),严格模式下为undefined;箭头函数无this,继承外层作用域;call/apply/bind可手动指定this,bind返回的新函数this不可再被call/apply更改;对象方法赋值后调用会丢失this绑定;new调用时this指向新创建的实例。
直接调用 fn(),非严格模式下 this 指向全局对象(浏览器中是 window),严格模式下是 undefined。这是最容易出错的场景——你以为在对象方法里写了个函数,结果忘了绑定,this 就飞了。
use strict 时,function foo() { console.log(this); } foo(); 输出 window
use strict,同样调用会报错或输出 undefined(取决于上下文)this,而是继承外层作用域的 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 返回的函数无法再被 call 或 apply 改变 this(除非用 new 调用)bind 的第一个参数是 null 或 undefined,非严格模式下仍会被转为全局对象当把对象方法赋值给变量或作为回调传入时,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 = () => {...})用 ne 调用函数时,引擎会创建一个新对象,把
wthis 绑定到该对象,并让其原型指向构造函数的 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
this,和普通函数一致;class 构造器中 this 才是新实例onClick、Lodash 的 debounce)时,得时刻问一句:这个函数被谁调用?this 是谁?