Junior — Senior
Custom implementation of the bind function
livecode
Task condition
You need to write a function myBind(fn, context) that returns a new function. When this new function is called, the original function fn should execute in the provided context and accept any arguments passed to it.
const obj = {
a: 1,
show(num) {
if (num !== undefined) {
console.log(this.a + num);
return;
}
console.log(this.a);
},
};
const fn = obj.show.bind(obj);
fn();
myBind(fn, obj)();
The example demonstrates how the standard bind attaches the show method to the obj object. Your task is to implement a similar mechanism without using the built-in Function.prototype.bind. The function should work correctly both with and without arguments.