Consider this code.
let array = [1,2,3,4,5]
for(let elem of array) {
console.log(elem)
}
Since here, I am not calling anything like array[Symbol.iterator]().since we can only execute function by putting parenthesis after the expression that evaluates its value. here we are just writing for(let elem of array) how does it execute a function named array[Symbol.iterator] ?
You can test it simply enough by replacing [Symbol.iterator] and see what happens:
let array = [1,2,3,4,5]
array[Symbol.iterator] = function* () {
yield *['Larry', 'Mo', 'Curley'];
};
for(let elem of array) {
console.log(elem)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With