From 51fcc451b1cc8a500cae4c017cc51ea21063b789 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sun, 12 Jul 2020 15:20:21 +0530 Subject: [PATCH] update post: How does in-built Array iterator work? --- ckciw2lr900d0xas144h5fpwm.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ckciw2lr900d0xas144h5fpwm.md b/ckciw2lr900d0xas144h5fpwm.md index 2f49048..f5d3de1 100644 --- a/ckciw2lr900d0xas144h5fpwm.md +++ b/ckciw2lr900d0xas144h5fpwm.md @@ -46,10 +46,10 @@ Now that we have the in-built array iterator, the most important thing is knowin ( Say, `arr` = [[IteratedArrayLike]], `index` = [[ArrayLikeNextIndex]], `kind` = [[ArrayLikeIterationKind]] ) -0. If `arr` is `undefined`, return `{value: undefined, done: true}` -(This is a special step, caused by step 2.a. It will reach here if you call `.next()` even after iterator has finished.) +1. If `arr` is `undefined`, return `{value: undefined, done: true}` +(This is a special step, caused by step 3.a. It will reach here if you call `.next()` even after iterator has finished.) -1. If `index < arr.length`, ( i.e. while values are available) +2. If `index < arr.length`, ( i.e. while values are available) a. set `key` = `index` b. If `kind` is `“key”`, return `{value: key, done: false}` @@ -66,10 +66,10 @@ Now that we have the in-built array iterator, the most important thing is knowin f. Set [[ArrayLikeNextIndex]] = index + 1 (⭐️ 1️⃣ Always increment key to next index. This sequential index is used to get the next value, irrespective of holes in that position (whether that index exists or not). It doesn't skip the empty/non-existent indexes.) -2. Else, (i.e. when `index >= arr.length` - reached end of array) +3. Else, (i.e. when `index >= arr.length` - reached end of array) a. Set `[[IteratedArrayLike]]` = `undefined`. - (⭐️ 2️⃣ Yes, once it reaches the end - it sets linked array to undefined. This is to ensure the once the iterator has finished, it will never return any more value. This undefined array is handled in step 0. + (⭐️ 2️⃣ Yes, once it reaches the end - it sets linked array to undefined. This is to ensure the once the iterator has finished, it will never return any more value. This undefined array is handled in step 1. If this was not done and if array length was increased before next call, then it would again return new values after saying `done:true` earlier.) b. Return `{value: undefined, done: false}`