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.

Lesson 2 of 38 steps

Method extraction gotchas, partial application is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

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
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

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

Yes — any 3 lessons of the JavaScript Academy learning path, including “Method extraction gotchas, partial application”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

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