This content originally appeared on DEV Community and was authored by Abhishek Raj
Introduction
As Javascript community keeps adding new methods in Javascript, so all browsers doesn't support new JS methods.
To make your JS code run on every browser, you need to add it on your own or you can use Babel, CoreJS. Sometimes companies ask in interview for Polyfills to know your understanding.
In this article, I will list out some Polyfills asked by companies.
1. Array Flat
This method is used to flat a nested array.
In the below example, we have used recursion to solve this problem. We have created 2 cases:
- A base case: If depth is reached then push arr in output and return it.
- A recursion case: Loop over array and check if its an Array or not. If its an array flat it, else push the number in output.
2. Array Filter
This is a Higher Order Function which takes another function and filters the array on the basis of the function.
In the below example, we have created a higher order function, which takes another function and calls on each element of the array. If it returns true then that element is inserted into result.
3. Array Reduce
This is a Higher Order Function which takes another function and reduces the array to single value/object and returns it.
In the below example, we have created a higher order function, which takes another function and calls on each element of the array. It mutates the result returned by the callback function.
4. Function Bind
The bind method is used to pass an execution context to the function.
In the below example, the custom Bind function takes the context and uses apply method to bind the function with the given context.
For more Awesome polyfills asked in interviews, check out JSVault
Connect Me @ Linkedin, Github, Twitter, Youtube ?
Note: I have written all the methods as pure functions, as I have only wanted to show the code. We can also use Prototypal Inheritance.
All the URLs in this post are powered by Sotly.co
This content originally appeared on DEV Community and was authored by Abhishek Raj
Abhishek Raj | Sciencx (2021-08-14T07:44:20+00:00) JS Polyfills asked in Interviews. Retrieved from https://www.scien.cx/2021/08/14/js-polyfills-asked-in-interviews/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.