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.

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

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