词法 this 与动态 this、箭头函数再探
了解动态 this 如何取决于调用位置,以及箭头函数如何捕获词法 this;使用小而清晰的示例进行练习
词法 this 与动态 this、箭头函数再探 是 CoddyKit 上的免费 JavaScript Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 JavaScript Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 JavaScript Academy 课程共包含 3 节课。
总体概览
目标:了解 this 在什么情况下指向哪里。
- 动态 this:由调用位置决定
- 箭头函数中的 this:按词法捕获
- 常见陷阱:提取方法会丢失 this
- 在回调中使用箭头函数来保留 this

动态 this 示例
动态 this:以 obj.method() 的形式调用会将 this = obj;提取函数则会丢失对象。
// Dynamic this depends on HOW we call the function
const person = {
name: "Ayla",
say: function () {
return "Hi " + this.name;
}
};
console.log("method call:", person.say()); // "Hi Ayla"
// Extract then call: this is lost
const talk = person.say;
console.log("extracted call:", talk()); // likely "Hi undefined" in strict mode -> this is undefined

箭头函数 = 词法 this
arrow 会在创建时从周围的函数或对象作用域获取 this;调用时不会改变它。
// Arrow functions capture this from where they are created
const box = {
label: "blue",
showLater: function () {
// Arrow keeps this === box
const fn = () => "box:" + this.label;
return fn();
}
};
console.log("arrow captured this:", box.showLater()); // "box:blue"

回调:使用箭头函数保留 this
在回调中,normal function 经常会丢失 this;arrow 则会完整保留外层的 this。
// Using a normal function in a callback can lose this
const widget = {
id: 7,
normal: function () {
setTimeout(function () {
// here this is not widget in many environments
console.log("normal cb this.id:", this && this.id);
}, 0);
},
arrow: function () {
setTimeout(() => {
// arrow keeps lexical this from arrow()
console.log("arrow cb this.id:", this.id);
}, 0);
}
};
widget.normal();
widget.arrow();

解决提取问题的简单方法
如果提取了方法,请使用捕获对象的小型箭头函数来包装它,或使用 bind(下一课介绍)。对于初学者,请保持简单。
// If you must extract, wrap with an arrow capturing the object
const account = {
owner: "Mila",
label: function () { return "Owner:" + this.owner; }
};
// Lost this:
const lost = account.label;
console.log("lost:", lost()); // "Owner:undefined" (this missing)
// Fixed: create an arrow that closes over account
const safe = () => account.label();
console.log("fixed via arrow wrapper:", safe());

良好习惯
提示:
- 方法:使用 obj.method() 调用,使 this 指向该对象。
- 回调:优先使用箭头函数来保留词法 this。
- 提取的方法会丢失 this;请使用箭头函数包装或 bind。

箭头函数中的 this 测验
快速检查:箭头函数中 this 的行为。

回顾
回顾:对于 normal function,this 是动态的(由调用位置设置);对于箭头函数,this 是词法的(在创建位置捕获)。在回调中使用箭头函数,并包装提取的方法,使行为保持简单。

常见问题解答
「词法 this 与动态 this、箭头函数再探」课时是免费的吗?
是的 — 「词法 this 与动态 this、箭头函数再探」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 JavaScript Academy 课程的其余内容,请升级到 CoddyKit PRO。 JavaScript Academy 课程共包含 3 节课。
「词法 this 与动态 this、箭头函数再探」这节课中我会学到什么?
了解动态 this 如何取决于调用位置,以及箭头函数如何捕获词法 this;使用小而清晰的示例进行练习 你通过在浏览器中直接运行的动手代码来练习 JavaScript Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 JavaScript Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 JavaScript Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。
「词法 this 与动态 this、箭头函数再探」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 JavaScript Academy 课中编写并运行代码吗?
能。每节 JavaScript Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 词法 this 与动态 this、箭头函数再探
- 方法提取陷阱与偏函数应用
- 函数式替代方案——传递上下文、闭包与小型辅助工具