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

Call-Site Determines 'this', Not Definition Site

Reinforce that 'this' binding depends on how a function is called, not where it's defined.

Call-Site Determines 'this', Not Definition Site

You'll learn: Why where a function is called matters more than where it's defined when determining this.

The Core Idea

One of the most critical mental shifts with this is understanding that the call-site (where you invoke the function) determines the value of this, not where you wrote the function. Unlike lexical variables that are resolved at definition time, this is resolved at runtime based on how the function is called.

Think of this like a chameleon: it changes color based on its surroundings at the moment of the call, not based on where the chameleon was born.

Why This Matters

You might define a function inside an object, but if you extract and call it elsewhere, this changes:

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

person.greet();           // 'Alice' — call-site is person.greet()

const extracted = person.greet;
extracted();              // undefined (strict) — call-site is plain function call

Same function definition, different call-sites, different this values.

Definition Site vs Call-Site

function sayHello() {
  console.log(this.message);
}

// Definition site doesn't matter
const obj1 = { message: 'Hello', sayHello };
const obj2 = { message: 'Hola', sayHello };

obj1.sayHello();  // 'Hello' — call-site: obj1
obj2.sayHello();  // 'Hola' — call-site: obj2

The function sayHello is defined once, but this adapts to whoever calls it.

Key Takeaway: The value of this is determined dynamically at the call-site, not statically where the function is written. Always look at how and where the function is invoked to predict this.