Putting aside concerns about modifying JavaScript's Array.prototype for now, how would I go about writing a custom method on the Array prototype that returns an iterator in reverse? Specifically, I'd like to provide an entriesReverse() method that does exactly what Array.prototype.entries() does except in reverse order. I've got as far as a bespoke function:
const entriesReverse = (array) => ({
[Symbol.iterator]() {
let index = array.length;
return {
next: () => ({
value: [--index, array[index]],
done: index < 0
})
}
}
})
but I'll be darned if I can figure out how to implement this as a method on the existing Array prototype. The problem seems to be trying to refer to 'this' inside the Symbol.iterator block - it doesn't refer to the array itself but to the iterator. Any thoughts?
thisargument.