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

Extracting Methods as Standalone Functions

Learn what happens when you reference a method without calling it and how 'this' is lost.

Extracting Methods as Standalone Functions

You'll learn what happens when you take a method out of its object and use it as a regular function—and why this disappears.

When you extract a method from an object and assign it to a variable or pass it around, you're creating a reference to the function without its object context. The method doesn't "remember" which object it came from—this becomes undefined (in strict mode) or the global object (in sloppy mode).

The Core Problem

Imagine handing someone a business card that says "Ask my assistant for my schedule." If you hand over just the card without the assistant nearby, it's useless. Similarly, when you extract a method, you're handing over the function without the object that this refers to.

const person = {
  name: 'Alice',
  greet() {
    console.log(`Hello, I'm ${this.name}`);
  }
};

person.greet(); // Works fine: "Hello, I'm Alice"

const greetFunction = person.greet; // Extract the method
greetFunction(); // Error or undefined! 'this' is lost

The call person.greet() binds this to person, but greetFunction() is called without any object, so this has no context.

Why This Matters

This commonly trips people up when passing methods as callbacks:

const user = {
  id: 42,
  logId() {
    console.log(this.id);
  }
};

setTimeout(user.logId, 1000); // 'this' will be lost!

You've seen solutions before (like bind() or arrow functions), but understanding why extraction breaks this is crucial.

Key Takeaway: Extracting a method from an object creates a standalone function that loses its this binding—the function no longer knows which object it belonged to.