What is the difference between the basic and advanced bind polyfill?
Basic: just sets this. Advanced: supports preset arguments (partial application) and the new operator (this is the new object when called with new, not the bound context).
Explore This Question
Search for additional explanations using these tools:
More FAQs in How to Write a Polyfill for bind in JavaScript
Function.prototype.myBind = function(context, ...presetArgs) { const fn = this; return function(...laterArgs) { return fn.call(context, ...presetArgs, ...laterArgs); }; }.
Use rest parameters: myBind = function(context, ...presetArgs). Spread them in the call: fn.call(context, ...presetArgs, ...laterArgs). This merges preset and later arguments.
Check this instanceof boundFn. If true (called with new), use new fn(...args) instead of fn.call(context, ...args). Set boundFn.prototype = Object.create(fn.prototype).
Still have questions?
Browse all our FAQs or reach out to our support team
