Back to articles
Polyfills

Polyfills

via Dev.to JavaScriptKush Bhandari

What are Polyfills? Polyfills are custom implementation to the common javascript methods. There can be cases where older browser is not supporting the latest javascript methods.So there we write polyfills. For Example: Polyfill for map method // Array.map using Array Method let array = [ 10 , 20 , 30 , 40 , 50 ]; let result = array . map ( item => item * 2 ); console . log ( " Result " , result ); // Our Prototype for map Array . prototype . myMap = function ( fn ){ if ( typeof fn !== " function " ){ throw new Error ( " Passed parameter is not function " ); } let array = this ; let output = []; for ( let i = 0 ; i < array . length ; i ++ ){ output . push ( fn ( array [ i ])); } return output } console . log ( array . myMap ( item => item * 2 )); I think you will be able to write for filter method as well. let me write for reduce. Polyfill for reduce method let array = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]; let defaultValue = 0 ; let result = array . reduce (( sum , acc ) => sum + acc , default

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles