Facebook Pixel

call, apply, and bind: Interview Preparation Guide

Everything you need to know about call, apply, and bind for interviews.

call, apply, and bind: Interview Preparation Guide

What to Know

  1. What each does: call (immediate, comma args), apply (immediate, array args), bind (returns function).
  2. How to implement: be able to write polyfills for each.
  3. Use cases: method borrowing, fixing this, partial application.
  4. Precedence: new > bind > call/apply > implicit > default.
  5. Arrows: ignore all three (lexical this).
  6. Re-binding: not possible (first bind wins).

Practice Questions

  1. Predict the output of code using call/apply/bind.
  2. Implement a polyfill for bind.
  3. Explain when to use call vs apply vs bind.
  4. Fix a lost-this bug using bind.
  5. Borrow an array method using call.

Common Interview Format

// Given: const obj = { x: 10 }; function foo(a, b) { return this.x + a + b; } // Predict: foo.call(obj, 1, 2); // 13 foo.apply(obj, [1, 2]); // 13 const bound = foo.bind(obj, 1); bound(2); // 13

The Takeaway

For interviews: know what each does, implement polyfills, know use cases, understand precedence, know arrows ignore them, and know re-binding is not possible. Practice output prediction and polyfill implementation.

What each does (call: immediate comma args, apply: immediate array args, bind: returns function), how to implement polyfills, use cases (method borrowing, fixing this, partial application), precedence (new > bind > call > implicit > default), arrows ignore them, and re-binding is not possible.

Return a new function that calls the original with call: Function.prototype.myBind = function(context, ...presetArgs) { return (...args) => this.call(context, ...presetArgs, ...args); }.

Predict the output of code using call/apply/bind with a specific this and arguments. For example: foo.call(obj, 1, 2) where foo returns this.x + a + b and obj.x is 10. Answer: 13.

new (highest) > bind > call/apply > implicit (method call) > default (plain call). Arrow functions are an exception: they use lexical this and ignore all of these rules.

No. The first bind wins. Once a function is bound, calling bind again on the bound function does not change this. This is a common interview trick question.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.