I have the following proxy:
const p = new Proxy({
[Symbol.iterator]: Array.prototype.values,
forEach: Array.prototype.forEach,
}, {
get(target, property) {
if (property === '0') return 'one';
if (property === '1') return 'two';
if (property === 'length') return 2;
return Reflect.get(target, property);
},
});
It's an array-like object, because it has numeric properties and the length property specifying the amount of elements. I can iterate it using a for...of loop:
for (const element of p) {
console.log(element); // logs 'one' and 'two'
}
However, the forEach() method is not working.
p.forEach(element => console.log(element));
This code doesn't log anything. The callback function is never called. Why isn't it working and how can I fix it?
Code snippet:
const p = new Proxy({
[Symbol.iterator]: Array.prototype.values,
forEach: Array.prototype.forEach,
}, {
get(target, property) {
if (property === '0') return 'one';
if (property === '1') return 'two';
if (property === 'length') return 2;
return Reflect.get(target, property);
},
});
console.log('for...of loop:');
for (const element of p) {
console.log(element);
}
console.log('forEach():');
p.forEach(element => console.log(element));
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.min.js"></script>