Facebook Pixel

bind Polyfill Interview Questions

Common interview questions about the bind polyfill.

bind Polyfill Interview Questions

Q1: Implement a polyfill for bind.

Function.prototype.myBind = function (context, ...presetArgs) { const fn = this; return function (...laterArgs) { return fn.call(context, ...presetArgs, ...laterArgs); }; };

Q2: How would you support the new operator?

Check this instanceof boundFn. If true, use new fn(...args).

Q3: What happens if you call myBind on an arrow function?

The arrow's this is lexical. call cannot change it. So the polyfill would not work for arrows (same as the real bind).

Q4: How do you handle preset arguments?

Use rest parameters: myBind = function(context, ...presetArgs). Spread them in the returned function: fn.call(context, ...presetArgs, ...laterArgs).

Q5: What is the time complexity of bind?

bind itself is O(1) (just creates a closure). The returned function has the same complexity as the original function.

The Takeaway

bind polyfill questions: implement from scratch, support new (instanceof check), handle preset args (rest/spread), arrows (cannot be bound), and complexity (O(1) for bind, same as original for the returned function).

Function.prototype.myBind = function(context, ...presetArgs) { const fn = this; return function(...laterArgs) { return fn.call(context, ...presetArgs, ...laterArgs); }; }.

Check this instanceof boundFn. If true, return new fn(...presetArgs, ...laterArgs). Otherwise, return fn.call(context, ...presetArgs, ...laterArgs).

No. Arrow functions have lexical this. call (used inside the polyfill) cannot change an arrow's this. This is the same behavior as the native bind.

Use rest parameters: myBind = function(context, ...presetArgs). Spread them in the returned function: fn.call(context, ...presetArgs, ...laterArgs). This merges preset and later arguments.

bind itself is O(1) - it just creates a closure. The returned function has the same time complexity as the original function. bind does not add any algorithmic overhead.

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.