What is this inside the myBind function in a bind polyfill?
this is the function on which myBind is called. For greet.myBind(obj), this is greet. Store it: const fn = this, and use it in the returned function.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in bind Polyfill: Step-by-Step Explanation
presetArgs are passed to myBind: greet.myBind(obj, 'Hello'). laterArgs are passed when the bound function is called: bound('!'). Both are spread into fn.call: fn.call(context, ...presetArgs, ...laterArgs).
To support the new operator. When a bound function is called with new, this should be the new instance, not the bound context. this instanceof boundFn is true only when called with new.
new boundFn() would set this to the bound context instead of a new instance. This is incorrect behavior. The real bind supports new, so the polyfill should too.
Still have questions?
Browse all our FAQs or reach out to our support team
