Here’s a simplistic implementation of the reduce method in Javascript.
Object.defineProperty(Array.prototype, "reduce", { enumerable: false, writable: true, value: function(cb, initialValue) { let result = initialValue; for (let i = 0; i < this.length; i++) { result = cb(result, this[i]); } return result; } });
And here’s how you can test it:
let result = [1, 2, 3, 4, 5].reduce((acc, curr) => { return acc + curr }, 0); console.log(result);
You can either grab this code and paste it in the console tab of your Inspector or you can use this Codepen link to try it out.
I’ll update my post very soon with further explanation on how this works. Stay tuned! For now you can find the Polyfill and read more about the reduce method in the Mozilla developer website.