Polyfills in JavaScript
Assalamu alaykum, everyone!
I haven't been able to write anything for a long time. I hope you will forgive me for this. I put it off for a long time thinking that it won't last long. I'm back again.
Starting today, I would like to enlighten you on the complicated topics in JavaScript that I have encountered in my interviews. Topics like these are not that difficult to talk about, it's just that the words may be unfamiliar! So don't hesitate in these matters.
So today's topic will be about polyfills. Have you ever come across this word before? If this is a new topic for you, let me tell you a little bit about what I know.
A polyfill is a piece of code designed to add or modify functions, providing modern functionality to web browsers. It serves as a means to supplement or enhance the capabilities of browsers.
In essence, a polyfill is code that extends or updates the capabilities of a web browser. It is used to introduce or modify features and functions in situations where they may not be natively supported.
Think of it as a contingency plan for when a web browser lacks support for certain functions. If, for instance, your browser does not offer the "map()" function, you would need to write your own version of "map()". This involves creating custom implementations for functions like "map()", "forEach()", and "reduce()".
To create these custom implementations, you typically define them within the Array.prototype, ensuring that they can be accessed and used with any array you declare in your code.
领英推荐
OK! I think everything is clear! Now imagine you're in an interview and you're asked to create a custom polyfill for a map.
What will you do? Based on the comments above, you can think about the map function.
What does this function actually do?
When and for what purpose we might need this function?
If you have enough knowledge about the function it won't be too difficult for you. If you are not familiar with such commonly used functions, I suggest you review these topics.
Let me show examples!
I think you understood what to do when you are asked to write your own polyfills!
Let's create your own polyfills for filter() and reduce() methods of array!
Let's read more about polyfills here.
Thanks for your attention!