From 145e76d3b318eab8ff78980c57f34e622a1988b2 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Mon, 17 Jun 2019 14:52:58 +0530 Subject: Iterators and Generators --- typescript.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/typescript.html.markdown b/typescript.html.markdown index ba4a9e71..9f0f4c6d 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -223,6 +223,25 @@ moreNumbers[5] = 5; // Error, elements are read-only moreNumbers.push(5); // Error, no push method (because it mutates array) moreNumbers.length = 3; // Error, length is read-only numbers = moreNumbers; // Error, mutating methods are missing + +// Iterators and Generators + +// for..of statement +// iterate over the list of values on the object being iterated +let list = [4, 5, 6]; +for (let i of list) { + console.log(i); // "4", "5", "6" +} + +// for..in statement +// iterate over the list of keys on the object being iterated +for (let i in list) { + console.log(i); // "0", "1", "2", +} + + + + ``` ## Further Reading -- cgit v1.2.3 From 1b67ecb30c8c634d7f1688696979798e658ca936 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Mon, 17 Jun 2019 14:56:28 +0530 Subject: Add one more example --- typescript.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/typescript.html.markdown b/typescript.html.markdown index 9f0f4c6d..827f202a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -228,6 +228,11 @@ numbers = moreNumbers; // Error, mutating methods are missing // for..of statement // iterate over the list of values on the object being iterated +let arrayOfAnyType = [1, "string", false]; +for (let val of arrayOfAnyType) { + console.log(val); // 1, "string", false +} + let list = [4, 5, 6]; for (let i of list) { console.log(i); // "4", "5", "6" -- cgit v1.2.3 From ff3dd723599190ccb1b5454f297d116b5c446c70 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Tue, 18 Jun 2019 12:21:00 +0530 Subject: PR changes: use const instead of let --- typescript.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/typescript.html.markdown b/typescript.html.markdown index 827f202a..55d8cefe 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -229,18 +229,18 @@ numbers = moreNumbers; // Error, mutating methods are missing // for..of statement // iterate over the list of values on the object being iterated let arrayOfAnyType = [1, "string", false]; -for (let val of arrayOfAnyType) { +for (const val of arrayOfAnyType) { console.log(val); // 1, "string", false } let list = [4, 5, 6]; -for (let i of list) { +for (const i of list) { console.log(i); // "4", "5", "6" } // for..in statement // iterate over the list of keys on the object being iterated -for (let i in list) { +for (const i in list) { console.log(i); // "0", "1", "2", } -- cgit v1.2.3