Skip to content

Instantly share code, notes, and snippets.

@abhinavnigam2207
Last active November 5, 2021 03:31
Show Gist options
  • Save abhinavnigam2207/af3d494524568a231caf6794d61f7d23 to your computer and use it in GitHub Desktop.
Save abhinavnigam2207/af3d494524568a231caf6794d61f7d23 to your computer and use it in GitHub Desktop.
Call Polyfill
Function.prototype.myCall = function(context, ...args) {
context.fnName = this;
context.fnName(...args);
}
/* Usage*/
function showProfileMessage(message) {
console.log(message, this.name);
}
const obj = {
name: "Abhinav Nigam"
};
showProfileMessage.myCall(obj, "welcome ");
@vvscode
Copy link

vvscode commented May 14, 2020

it doesn't show welcome =)

@Volodymyrprodyus
Copy link

Maybe it will be more correct:
Function.prototype.myCall = function(context, ...args) {
context.fnName = this;
context.fnName(...args);
}

@abhinavnigam2207
Copy link
Author

Thanks @Volodymyrprodyus for pointing out. Updated the gist

@akhilakonam
Copy link

akhilakonam commented Sep 24, 2020

fnName should be deleted after its execution as call doesn't edit the context we pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment