0Pricing
JavaScript Academy · Lesson

Lexical vs dynamic this, arrows revisited

See how dynamic this depends on call-site, and how arrow functions capture lexical this; use tiny, clear examples.

Lexical vs dynamic this, arrows revisited is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Big picture

Goal: Know when this points where.

  • Dynamic this: decided by call-site
  • Arrow this: captured lexically
  • Common gotcha: method extraction loses this
  • Use arrows to keep this inside callbacks
Lexical vs dynamic this, arrows revisited — illustration 1

Dynamic this demo

Dynamic this: calling as obj.method() sets this = obj; extracting the function loses the object.

// 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
Lexical vs dynamic this, arrows revisited — illustration 2

Arrow = lexical this

An arrow takes this from the surrounding function/object scope when created; it does not change at call time.

// 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"
Lexical vs dynamic this, arrows revisited — illustration 3

Callbacks: keep this with arrows

In callbacks, a normal function often loses this; an arrow keeps the outer this intact.

// 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();
Lexical vs dynamic this, arrows revisited — illustration 4

Simple fix for extraction

If a method is extracted, wrap a small arrow that closes over the object, or bind it (next lesson). Keep it simple for beginners.

// 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());
Lexical vs dynamic this, arrows revisited — illustration 5

Good habits

Tips:

  • Methods: call with obj.method() so this is the object.
  • Callbacks: prefer arrows to keep lexical this.
  • Extracted methods lose this; wrap with an arrow or bind.
Lexical vs dynamic this, arrows revisited — illustration 6

Arrow this quiz

Quick check: Arrow this behavior.

Lexical vs dynamic this, arrows revisited — illustration 7

Recap

Recap: this is dynamic for normal functions (set by call-site) and lexical for arrows (captured where created). Use arrows in callbacks and wrap extracted methods to keep behavior simple.

Lexical vs dynamic this, arrows revisited — illustration 8

Frequently asked questions

Is the “Lexical vs dynamic this, arrows revisited” lesson free?

Yes — the full text of “Lexical vs dynamic this, arrows revisited” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Lexical vs dynamic this, arrows revisited”?

See how dynamic this depends on call-site, and how arrow functions capture lexical this; use tiny, clear examples. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Lexical vs dynamic this, arrows revisited” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lexical vs dynamic this, arrows revisited
  2. Method extraction gotchas, partial application
  3. Functional alternatives — pass context, closures, tiny helpers
← Back to JavaScript Academy