在 javascript 的 class 中,this 指向取决于方法的调用方式。对于普通实例方法,this 指向调用该方法的对象实例;对于静态方法(static),this 指向类本身而非实例;若方法被提取为独立函数调用且未绑定上下文,this 将变为 undefined(严格模式)或全局对象(非严格模式)。

实例方法中 this 如何指向当前对象
当通过实例调用普通方法时,this 自动绑定到该实例。这是最常见的场景,无需额外处理即可访问实例属性和其他方法。

class User {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, I'm ${this.name}`);
}
}
const user = new User('Alice');
user.greet(); // Hello, I'm Alice预期输出:
Hello, I'm Alice
验证说明:user.greet() 调用时,this 指向 user 实例,因此能正确读取 this.name 并输出


















