/**
* Read FAQs section on the left for more information on how to use the editor
**/
Function.prototype.customBind = function (context={},...args) {
if(typeof this !== 'function') {
throw new Error(this+ "It's not callable");
}
context.fn = this;
return function(...newArgs) {
return context.fn(...args,...newArgs);
}
}
const module = {
x: 42,
getX: function() {
return this.x;
}
}
const unboundGetX = module.getX;
console.print(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined
const boundGetX = unboundGetX.customBind(module);
console.print(boundGetX());
// expected output: 42
Read-only