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); }, }); 
This is a massive object because it has numeric properties and a length property that determines the number of elements. I can iterate with a for...of loop:
 for (const element of p) { console.log(element); // logs 'one' and 'two' } 
However, the forEach() method does not work.
 p.forEach(element => console.log(element)); 
This code does not write anything. The callback function is never called. Why is this not 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> 
source share