0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to implement your own version of .bind method? | JavaScript Interview Question | Bind Polyfill

@Yomesh Gupta
301

You have to create a polyfill for .bind.

const module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}

const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined

const boundGetX = unboundGetX.customBind(module);
console.log(boundGetX());
// expected output: 42

To test and run your code, add the following in your code

const module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}

const boundGetX = unboundGetX.customBind(module);
// Output should be 42.
printOutput(boundGetX());

Loading IDE...