Why is the new operator support important in a bind polyfill?
Because the real bind supports new. If a bound function is called with new, this should be the new instance, not the bound context. Without new support, new boundFn() would incorrectly use the bound context as this.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in bind Polyfill Common Mistakes
Because this inside the returned function is different from this inside myBind. Store it: const fn = this. Use fn in the returned function. Without this, the polyfill cannot call the original function.
Because arrow functions do not have their own this. The instanceof check (this instanceof boundFn) would not work with arrows. Use a regular function for the returned function.
The original function would not receive the arguments passed when the bound function is called. For greet.myBind(obj, 'Hello')('!'), the '!' would be lost. Always spread laterArgs: fn.call(context, ...presetArgs, ...laterArgs).
Still have questions?
Browse all our FAQs or reach out to our support team
