๐ Frontend DSA 10 - ๐ฅ JavaScript Polyfill: Array.prototype.map()
JavaScriptโs built-in Array.prototype.map()
method creates a new array by calling a provided callback function on every element in the calling array.
Letโs write a polyfill that mimics this behavior and call it myMap()
.
โ Working Solution:
const test = [1, 2, 3, 4, 5, 6, 7, 8, 8, 10];
// Polyfill for map
Array.prototype.myMap = function (callbackFun) {
let result = [];
let currentArray = this;
for (let i = 0; i < currentArray.length; i++) {
// Apply callback with (element, index, array)
result[i] = callbackFun(currentArray[i], i, currentArray);
}
return result;
}
// Native map usage
let result1 = test.map((item, index) => {
return item * 2;
});
console.log("Native Map:", result1); // [2, 4, 6, ..., 20]
// Custom polyfill usage
let result2 = test.myMap((item, index) => {
return item * 2;
});
console.log("Polyfill Map:", result2); // [2, 4, 6, ..., 20]
Problem Statement
๐ Key Points
๐ง Why use this
?
In the line let currentArray = this;
, this
refers to the array on which myMap()
is called. It ensures the method is reusable for any array.
โ
Signature of Original .map()
Method
arr.map((element, index, array) => { /* ... */ })
โก๏ธ Your polyfill should match that: it must pass all 3 arguments to the callback.
๐งช Extra Test Cases
let names = ['John', 'Jane', 'Doe'];
console.log(
names.myMap((name, index) => `${index + 1}. ${name.toUpperCase()}`)
);
// [ '1. JOHN', '2. JANE', '3. DOE' ]
Code Block
๐ Edge Cases to Consider (Advanced)
To make your polyfill bulletproof like native .map()
:
- โ
Validate
callbackFun
is a function. - โ Preserve holes in sparse arrays.
- โ
Handle
thisArg
(optional second parameter). - โ Avoid mutating the original array.
But for basic interview-level implementation, what we did is sufficient. ๐ฏ
Hey there from LearnYard! ๐
Glad to see you exploring the free content from our course! Weโve shared 10 valuable articles at no cost to help you get a real feel of whatโs inside.
If youโve found these helpful, weโd love to know! ๐ฌ
Share your thoughts or key takeaways on LinkedIn, Twitter, or Instagram โ and donโt forget to tag Gourav Hammad & Mohammad Fraz. Weโll personally respond to your post!
Thanks for being part of the LearnYard journey. Keep learning, keep growing! ๐
Loved the free content? Want access to the full course โ for free? ๐ธ
Share the course with your friends using your affiliate link (youโll find it on your profile or home page), and earn 20% commission on each sale.
Just 4โ5 sales and youโll recover the full course price of your course ๐
So why wait? Start sharing, start earning, and unlock the rest of the course effortlessly! ๐ ๏ธ๐
๐ Keywords for SEO
- JavaScript
map()
Polyfill Explained - How to Create a Custom
map()
Function in JavaScript - Polyfill for
Array.prototype.map
in JS - JavaScript Prototype Method Interview Question
- Custom
map()
Function for Arrays in JavaScript - Implementing
map()
Manually in JavaScript - JavaScript Polyfill for Array Methods
- Build Your Own
map()
Function in JS - Understand
map()
Internals in JavaScript - JavaScript Prototype Inheritance and Custom Methods
๐ข Hashtags for Visibility
#JavaScriptPolyfill #MapFunctionJS #CustomMapJS #ArrayPrototypeJS
#JSInterviewPrep #FrontendInterviewQuestions #JavaScriptTips
#CodeNewbie #LearnJavaScript #FrontendMasters #PrototypeMethods
#JSForBeginners #WebDevelopment #FunctionalProgrammingJS
#JavaScriptDeepDive #CustomJSFunctions #JSArrayMethods
#FrontendDevelopment #TechLearning #JSCodeChallenge