0Pricing
JavaScript Academy · 课时

this 基础;call/apply/bind(简介)

了解 this 在函数和方法中指向什么;学习箭头函数中的 this,并初步认识 call/apply/bind。

this 基础;call/apply/bind(简介) 是 CoddyKit 上的免费 JavaScript Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 JavaScript Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 JavaScript Academy 课程共包含 3 节课。

this 简介

目标:了解在不同调用方式中 this 所指的对象,学习箭头函数中的 this,并逐步尝试 call/apply/bind。

  • 方法调用与普通调用
  • 箭头函数中的 this
  • call/apply/bind
this 基础;call/apply/bind(简介) — 插图 1

方法调用中的 this

在方法调用中,this 指向点号前的对象。

// Method call: this is the object before the dot
const user = {
  name: "Ayla",
  say() {
    console.log("I am", this.name);
  }
};

user.say();
this 基础;call/apply/bind(简介) — 插图 2

普通调用中的 this

在严格模式下,普通调用中的 this 是 undefined。在不使用严格模式的浏览器中,它可能是 window。

// Plain function call: this is undefined in strict mode
function show() {
  console.log("this is", this);
}

show();
this 基础;call/apply/bind(简介) — 插图 3

箭头函数中的 this(词法绑定)

箭头函数使用外层的 this(词法绑定)。它非常适合在方法内部使用的回调函数。

// Arrow functions do not have their own this
// They capture this from the surrounding scope
const team = {
  title: "Blue",
  start() {
    const report = () => {
      console.log("Team:", this.title);
    };
    report();
  }
};

team.start();
this 基础;call/apply/bind(简介) — 插图 4

call 与 apply

call 和 apply 会立即调用函数,并使用给定的 this。

// call/apply invoke the function with a chosen this
function intro(lang1, lang2) {
  console.log("User:", this.name, "knows", lang1, "and", lang2);
}

const person = { name: "Mina" };

// call passes arguments normally
intro.call(person, "JS", "TS");

// apply passes arguments as an array
intro.apply(person, ["JS", "TS"]);
this 基础;call/apply/bind(简介) — 插图 5

bind 基础

bind 会返回一个新函数,在后续调用中记住选定的 this。

// bind creates a new function with this fixed
function sayHi() {
  console.log("Hi from", this.name);
}

const dev = { name: "Lena" };

const sayHiFromLena = sayHi.bind(dev);

sayHiFromLena();
sayHiFromLena();
this 基础;call/apply/bind(简介) — 插图 6

bind 与 call/apply 小测

快速检查:固定 this。

this 基础;call/apply/bind(简介) — 插图 7

回顾

回顾:方法调用会将 this 设置为对象;严格模式下普通调用中的 this 是 undefined;箭头函数会捕获外层的 this;call/apply 会为一次调用设置它;bind 则会为后续调用固定它。

this 基础;call/apply/bind(简介) — 插图 8

常见问题解答

「this 基础;call/apply/bind(简介)」课时是免费的吗?

是的 — 「this 基础;call/apply/bind(简介)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 JavaScript Academy 课程的其余内容,请升级到 CoddyKit PRO。 JavaScript Academy 课程共包含 3 节课。

「this 基础;call/apply/bind(简介)」这节课中我会学到什么?

了解 this 在函数和方法中指向什么;学习箭头函数中的 this,并初步认识 call/apply/bind。 你通过在浏览器中直接运行的动手代码来练习 JavaScript Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 JavaScript Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 JavaScript Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「this 基础;call/apply/bind(简介)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 JavaScript Academy 课中编写并运行代码吗?

能。每节 JavaScript Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 声明与表达式、箭头函数基础
  2. 参数、默认值、剩余参数与返回值
  3. this 基础;call/apply/bind(简介)
← 返回 JavaScript Academy