// Asked in Jio and Expedia
// Write custome polyfill for call, apply and bind method ?
Function.prototype.myCall = function (context, ...args) {
// Ensure arguments are correct
if (typeof this !== "function") {
throw new TypeError("myCall must be called on a function");
context = context !== null && context !== undefined ? Object(context) : globalThis;
const uniqueSymbol = Symbol(); // it shoud be unique
context[uniqueSymbol] = this;
// Call the function with the provided arguments
const result = context[uniqueSymbol](...args);
// Remove the unique property to avoid side effects
delete context[uniqueSymbol];
const person = { name: "John" };
console.log(greet.myCall(person, "Alice"));
Function.prototype.myApply = function (context, args) {
if (typeof this !== "function") {
throw new TypeError("myApply must be called on a function");
// Ensure context is an object
context = context !== null && context !== undefined ? Object(context) : globalThis;
// Ensure args is an array
if (!Array.isArray(args)) {
throw new TypeError("The second argument must be an array");
const uniqueSymbol = Symbol();
context[uniqueSymbol] = this;
// Call the function with arguments spread out (if any)
const result = context[uniqueSymbol](...args);
delete context[uniqueSymbol];
console.log(sum.myApply(context, [5, 10]));
Function.prototype.myBind = function (context, ...boundArgs) {
if (typeof this !== "function") {
throw new TypeError("myBind must be called on a function");
// Ensure context is an object
context = context !== null && context !== undefined ? Object(context) : globalThis;
return function (...args) {
return fn.apply(context, [...boundArgs, ...args]);
function multiply(a, b) {
const boundMultiply = multiply.myBind(null, 2);
console.log(boundMultiply(3));
console.log(boundMultiply(4));