Solution

What the interviewer expects

You understand that bind internally uses apply, returns a closure, and supports partial application by merging pre-filled args with later args.

This separates memorizers from true understanders.

Implementation

Function.prototype.myBind = function(thisArg, ...args) {
  // 1. save reference to original fn, 
  // since we are in the context of the original function
  const fn = this;                          
  
  // 2. return a new function
  return function(...newArgs) {             
    // 3. merge pre-filled args with later args
    return fn.apply(thisArg, [...args, ...newArgs]); 
  };
};

Summary

  • myBind saves a reference to the original function.
  • It returns a new function that calls apply with the merged arguments.
  • This supports partial application by merging pre-filled args with later args.