Course contentsShow
← JavaScript
Lesson 649 of 2,72316. Object Methods and PatternsFree lesson

How 'this' Works in Method Calls

Explore how 'this' is dynamically bound to the calling object when invoking methods.

How 'this' Works in Method Calls

You'll learn how this automatically refers to the object that calls the method, making your methods reusable and context-aware.

The Core Idea

When you call a method on an object using dot notation (like person.greet()), JavaScript automatically sets this inside that method to refer to the object before the dot. This is called implicit binding — you don't have to manually pass the object in; JavaScript does it for you.

Think of this like a pronoun in conversation. When someone says "I am hungry," the word "I" automatically refers to whoever is speaking. Similarly, when a method runs, this automatically refers to whoever "called" it.

How It Works

const user = {
  name: "Alice",
  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

user.greet(); // "Hello, my name is Alice"
// 'this' inside greet() refers to 'user'

The call site user.greet() determines what this means. Because user is the object making the call, this equals user.

Multiple Objects, Same Method

const dog = {
  name: "Buddy",
  speak() {
    console.log(`${this.name} says woof!`);
  }
};

const cat = {
  name: "Whiskers",
  speak() {
    console.log(`${this.name} says meow!`);
  }
};

dog.speak(); // "Buddy says woof!"
cat.speak(); // "Whiskers says meow!"

Each call binds this to the respective object. The call site (where you actually invoke the method) determines the context, not where the method was defined.

Key Takeaway: In method calls, this is dynamically bound to the object before the dot at the call site, allowing the same method to work with different objects' data.