Skip to content
Shubham JangleResources
JavaScript

Explore topics

Polyfills

Watch the walkthrough
Open on Instagram

Revision clarifications

The imported newMap is an educational approximation, not a compliant Array.prototype.map polyfill. Its indexOf test mishandles NaN and it omits callback/receiver and array-length semantics. Its first snippet also uses numbers before declaring it. Run the usage declaration first; use a vetted polyfill for actual browser compatibility. Array.map semantics.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

Web standards and JavaScript APIs evolve over time, and newer features may not be available in older browsers. Polyfills enable developers to write modern code and still support users on older browsers. This is crucial for maintaining a wide user base without sacrificing modern development practices.

By leveraging polyfills, you can ensure a broader reach for your web applications and provide a consistent experience across different browsers and environments.

// Built-in Array .map() method
const d = numbers.map((item, index) => item * 2);
console.log(d); // [2, 4, 6, 8, undefined x 6, 68]
if (!Array.prototype.newMap) {
  Array.prototype.newMap = function (callback) {
    const result = [];
    const array = this;
    for (let i = 0; i < array.length; i++) {
      // check if array[i] is not undefined
      if (array.indexOf(array[i]) > -1) {
        result[i] = callback(array[i], i, array);
      }
    }
    return result;
  };
}

// usage example
const numbers = [1, 2, 3, 4]; // [1, 2, 3, 4]
numbers[10] = 34; // [1, 2, 3, 4, undefined x 6, 34]

const double = numbers.newMap((item, index) => item * 2);
console.log(double); // [2, 4, 6, 8, undefined x 6, 68]