DEV Community

Cover image for Polyfill for Call()
Chiranjit Dey
Chiranjit Dey

Posted on Edited on

Polyfill for Call()

Polyfills are essential tools for developers aiming to create web applications compatible with a wide range of browsers. These code snippets bridge the gap between modern functionalities and older browser limitations.

Today, we'll delve into the call() function and explore how to implement a polyfill for it, ensuring your applications function seamlessly across different browser environments.

Given below is a line by line explanation of the code.

  1. The call() method in JS invokes a function with a specified "this" value and allows passing arguments individually.

  2. Ln:1, person is an object.

  3. Ln:5, printAge is a function, that takes age as an arg. (Call method can take N number of arguments).

Polyfill for Call

4.Ln:9, Function.prototype.myCall is our polyfill that handles the call method. It takes the object and arguments.

  1. Ln:10, we now make a key in the object naming it fn and assigning "this" to it. "this" refers to the printAge function() here.

  2. Ln:11 we call the function with the arguments.

  3. Ln:14, we call the myCall method here and get to see our output in the console.

If you like the post, make sure to follow me on Twitter and LinkedIn. I post almost everyday about JS fundamentals.

Twitter
LinkedIn

Top comments (0)