Course contentsShow
← JavaScript
Lesson 535 of 2,72314. The 'this' KeywordFree lesson

Dynamic Binding Explained

How 'this' is determined by how a function is called, not where it's defined—the core of dynamic binding.

Dynamic Binding Explained

What you'll learn: How JavaScript determines what this refers to based on how a function is called, not where it's written.

The Core Idea

In most of JavaScript, where you write code determines what it can access—that's lexical scope, which you've already learned. But this works completely differently. It uses dynamic binding, meaning the value of this is determined at runtime based on how the function is invoked, not where it's defined.

Think of it like a pronoun in conversation. When you say "I am hungry," the word "I" refers to whoever is speaking at that moment—not who wrote the sentence. Similarly, this refers to whatever context is calling the function at execution time.

Why This Matters

const person = {
  name: 'Alice',
  greet: function() {
    console.log(this.name);
  }
};

person.greet(); // "Alice" - 'this' is person

const greetFunc = person.greet;
greetFunc(); // undefined or error - 'this' is now something else!

Same function, two different results. The function didn't change, but how it was called changed, so this changed.

This is fundamentally different from closures, which capture their scope when created and never change. With this, every single call can potentially bind to a different value.

The Key Rule

Dynamic binding means: Look at the function call, not the function definition. The call site determines this.

Key Takeaway: this is determined by how a function is called at runtime (dynamic binding), not where it's defined—making it fundamentally different from lexical scope and closures.