0Pricing
JavaScript Academy · Lesson

Method extraction gotchas, partial application

Fix method extraction with call/apply/bind and learn tiny partial application patterns with bind and wrapper arrows.

Big picture

Goal: Keep this stable after extraction and pre-fill args.

  • call(thisArg, a, b)
  • apply(thisArg, [a, b])
  • bind(thisArg, a) → returns new function
  • Partial application with bind or a tiny arrow
Method extraction gotchas, partial application — illustration 1

Extraction gotcha

Calling as obj.method() works, but extracting the function loses the object and breaks this.

// Extraction loses this
const user = {
  name: "Ayla",
  label: function (prefix) {
    return prefix + ":" + this.name;
  }
};

console.log("method call:", user.label("Hi")); // "Hi:Ayla"

// Extract
const f = user.label;
console.log("extracted:", f("Hi")); // this is missing -> "Hi:undefined" (in strict mode this is undefined)
Method extraction gotchas, partial application — illustration 2

All lessons in this course

  1. Lexical vs dynamic this, arrows revisited
  2. Method extraction gotchas, partial application
  3. Functional alternatives — pass context, closures, tiny helpers
← Back to JavaScript Academy