Polyfills & utilities · Build it
bind / call / apply polyfills
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Write Function.prototype.myBind. Make it work with new. Then do myCall without using call/apply.
Then
Follow-ups the interviewer asks next
newon the bound function must ignore the boundthis.myCall: attach the fn toctxunder aSymbolkey, invoke it, delete it.null/undefinedctx meansglobalThisin sloppy mode.- Preserve
prototypeandlength.
The answer
What a strong answer covers
- Function.prototype.myBind = function (ctx, ...bound) {
- const fn = this;
- function boundFn(...args) {
- return fn.apply(this instanceof boundFn ? this : ctx, [...bound, ...args]);
- }
- boundFn.prototype = Object.create(fn.prototype);
- return boundFn;
- };
- Partial application of the leading args
- Use a Symbol key in
myCallto avoid clobbering existing properties
The red pen
Common traps
Avoid:using an arrow inside so new breaks; mutating ctx with a string key.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.