bind polyfill
JavaScript
/* bind polyfill */
Function.prototype.bind = function (context, ...rest) {
return (...args) => this.apply(context, [...rest, ...args]);
}
/* tests */
const getName = function () {
return this.riba;
}
const obj1 = {
riba: 'obj-1'
};
const obj2 = {
riba: 'obj-2'
};
console.assert(getName.bind(obj1)() === 'obj-1', 'Name should be "obj-1"');
console.assert(getName.bind(obj2)() === 'obj-2', 'Name should be "obj-2"');