箭头函数没有自己的this,而是词法绑定外层普通函数或全局作用域的this值,该绑定在函数创建时确定且不可更改。

箭头函数不绑定自己的 this,而是**直接沿用定义时外层普通函数(或全局)作用域的 this 值**,这个行为发生在函数创建时,不是调用时。
为什么箭头函数没有自己的 this?
这是语言设计决定的:箭头函数的 this 是词法绑定(lexical binding),它在函数体被解析、函数对象被创建的那一刻就确定了,和外层第一个非箭头函数的 this 保持一致。它不会因为 .call()、.apply()、.bind() 或作为方法被调用而改变。
常见场景对比:普通函数 vs 箭头函数
看一个典型例子:
const obj = {
name: 'Alice',
regular() {
console.log('regular this:', this.name); // 'Alice'
setTimeout(function() {
console.log('normal function this:', this.name); // undefined(全局 this)
}, 100);
setTimeout(() => {
console.log('arrow function this:', this.name); // 'Alice' ✅
}, 100);
}
};
obj.regular();
- 普通函数中嵌套的
function(){}:有自己的this,调用时绑定到全局(非严格模式)或undefined(严格模式) - 同一位置的箭头函数:继承外层
regular()的this(即obj),所以能正确访问this.name
注意“外层”指的是定义时的外层,不是调用时的外层
箭头函数的 this 取决于它写在哪,而不是在哪被调用。例如:
在 Java 中初始化和管理阿里云 SDK客户端。包括单例模式、线程安全、endpoint 与 region 配置、VPC 终端节点、同步与异步等。
立即学习“Java免费学习笔记(深入)”;
function outer() {
console.log('outer this:', this.name); // 'Bob'
return () => {
console.log('arrow this:', this.name); // 始终是 'Bob',哪怕被别的对象调用
};
}
const obj1 = { name: 'Alice' };
const obj2 = { name: 'Bob' };
const arrow = outer.call(obj2); // outer 的 this 是 obj2 → 箭头函数的 this 锁定为 obj2
arrow.call(obj1); // 无效!this 仍是 obj2
- 箭头函数的
this在outer()执行并创建它时就固定了 -
.call(obj1)对箭头函数完全无效——它不响应任何显式this绑定操作
哪些情况不能靠箭头函数继承 this?
当外层根本没有有效的 this(比如顶层脚本、模块顶层、或外层也是箭头函数)时,箭头函数会继承到全局对象(浏览器中是 window)或 undefined(严格模式下):
'use strict'; const fn = () => console.log(this); // this 是 undefined fn(); // 模块文件顶层(ESM) const arrowInModule = () => this; // this 是 undefined(ESM 中顶层 this 是 undefined)
- 确保外层有一个你期望的
this绑定(通常是某个普通函数方法) - 避免在全局作用域或纯箭头链中依赖
this
不复杂但容易忽略:箭头函数的 this 是静态的、只读的、不可重绑定的——它让回调和嵌套逻辑更可预测,但也意味着你无法用传统方式动态控制它。

















