Call polyfill returns undefined

Viewed 25

I have created a polyfill of call function in javascript but it doesn't seem to work accordingly.

const myName = {
  firstName: "Test",
  lastName: "Tester"
};

function printName(city, country, funcName) {
  return `${funcName} => ${this.firstName} ${this.lastName}, ${city} - ${country}`;
};

console.log(printName.call(myName, "Kerala", "India", "Call"));
//Outputs - Call => Test Tester, Kerala - India 

// My polyfill

Function.prototype.myCall = function(context, ...args) {
  context.fn = this;
  context.fn(...args);
};

console.log(printName.myCall(myName, "Kolkata", "India", "myCall"));
//Outputs - undefined

I am not sure why my polyfill of call gives undefined although if console inside the printName it works fine? If anyone could help. Thanks in advance.

0 Answers
Related