JavaScript 词条目录
前端Wiki/词条/this

this

定义JavaScript 函数中的特殊关键字,指向函数执行时的上下文对象,取决于调用方式

this 是「运行时绑定」的:函数定义时不知道 this 是谁,只有被调用的那一刻才确定。判定规则按优先级:new 绑定 > 显式绑定(call/apply/bind)> 隐式绑定(对象方法调用)> 默认绑定(严格模式 undefined,非严格 window)。

箭头函数没有自己的 this,它继承外层词法作用域的 this——这正是它在回调中好用的原因,再也不用 var self = this 或 .bind(this) 了。

JavaScript
const obj = {
  name: 'A',
  greet() { setTimeout(() => console.log(this.name), 100); }
};
obj.greet();                    // A(箭头函数继承 obj 的 this)

function f() { console.log(this); }
f.call({ x: 1 });               // { x: 1 } 显式绑定
class C { m = () => this; }     // 类字段箭头函数永久绑定实例
⚠ 常见误区
🔗 相关词条this 指向作用域箭头函数闭包
← 返回来源章节js/g2c1s0.html