Function.prototype.myBind = function(context, ...presetArgs) { const fn = this; return function(...laterArgs) { return fn.call(context, ...presetArgs, ...laterArgs); }; }.
bind Polyfill Interview Questions
How do you implement a polyfill for bind?
The answer
More FAQs in bind Polyfill Interview Questions
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.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.