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

Method Calls: When 'this' is the Object

Learn how 'this' refers to the object when a function is called as a method using dot notation.

Method Calls: When 'this' is the Object

You'll learn how this gets bound to an object when you call a function as a method using dot notation.

The Core Idea

When a function is called as a method of an object (using dot notation), this inside that function automatically refers to the object that owns the method. This is one of JavaScript's most straightforward this binding rules.

Think of it like this: when you say "the car's engine starts," the engine belongs to that specific car. Similarly, when you call car.start(), the start function knows which car it's working with through this.

How It Works

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

person.greet(); // Logs: "Hello, I am Alice"
// 'this' refers to 'person' because greet was called on person

The key is the call-site: how the function is invoked. When you use object.method(), JavaScript binds this to object for that method call.

const calculator = {
  value: 0,
  add: function(n) {
    this.value = this.value + n;
    return this.value;
  }
};

calculator.add(5); // 'this' is 'calculator', so this.value is calculator.value
console.log(calculator.value); // 5

The dot before the function name tells JavaScript: "bind this to whatever's on the left side of the dot."

Key Takeaway: When a function is called as a method using dot notation (object.method()), this refers to the object before the dot—this is method call binding.