I am trying to create a polyfill for the call, apply and bind methods.
const user = {
firstName: "Christopher",
lastName: "Nolan",
};
const fullName = function (place, country) {
console.log(
`${this.firstName} ${this.lastName} is from ${place}, ${country}.`
);
};
// Using a call method
fullName.call(user, "London", "UK");
// Re-creating a call method with the name of "_call"
Function.prototype._call = function (...args) {
const funcObj = this;
const params = args.slice(1);
return (function () {
const obj = args[0];
// How can I point out the obj to "this" keyword inside a funcObj without using the bind method as I mentioned below.
return funcObj.bind(obj, ...params)();
})();
};
fullName._call(user, "London", "UK");
Function.prototype.call
andFunction.prototype.apply
were introduced in ECMAScript 3, in 1999/2000.Function.prototype.bind
was introduced in ECMAScript 5 in 2009/2011. Polyfillingbind
in terms of features that have been supported up to 10 years before makes sense. However, ECMAScript 3 is considered the lowest common denominator in terms of browser support, so polyfilling ES3 features isn’t that useful, and unlikely to be possible.