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.

Method extraction gotchas, partial application is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

call/apply once-off

call and apply let you set this for a single invocation.

// Fix by passing this explicitly using call/apply
console.log("call :", f.call(user, "Hi"));   // "Hi:Ayla"
console.log("apply:", f.apply(user, ["Yo"])); // "Yo:Ayla"
Method extraction gotchas, partial application — illustration 3

bind for reuse

bind creates a function that remembers this. Call it many times without re-binding.

// bind returns a new function with this fixed
const bound = f.bind(user);
console.log("bound once:", bound("Hey")); // "Hey:Ayla"
console.log("bound again:", bound("Yo")); // "Yo:Ayla"
Method extraction gotchas, partial application — illustration 4

Partial application (simple)

Use bind to pre-fill leading args, or a tiny arrow wrapper to freeze values and this.

// Partial application: pre-fill arguments
function add(a, b, c) { return a + b + c; }

// Using bind: pre-fill a=10
const add10 = add.bind(null, 10);
console.log("add10(1,2):", add10(1, 2)); // 13

// Using an arrow wrapper: pre-fill prefix
const labelHi = function (obj) {
  return function () { return obj.label("Hi"); };
}(user);

console.log("arrow partial:", labelHi()); // "Hi:Ayla"
Method extraction gotchas, partial application — illustration 5

Practical patterns

Tips:

  • Extracted methods: use bind once and reuse.
  • One-off calls: use call/apply.
  • Need pre-filled args? bind or a tiny arrow wrapper.
  • Keep helpers tiny and named clearly.
Method extraction gotchas, partial application — illustration 6

bind vs call/apply quiz

Quick check: Preserve this and pre-fill args.

Method extraction gotchas, partial application — illustration 7

Recap

Recap: Extraction loses this. Use call/apply for one-off calls, bind to fix this and reuse. For partials, use bind or a tiny arrow to pre-fill values.

Method extraction gotchas, partial application — illustration 8

Frequently asked questions

Is the “Method extraction gotchas, partial application” lesson free?

Yes — the full text of “Method extraction gotchas, partial application” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Method extraction gotchas, partial application”?

Fix method extraction with call/apply/bind and learn tiny partial application patterns with bind and wrapper arrows. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Method extraction gotchas, partial application” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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