From 9b28c13bcf8aef0ac5d5d09430229f8542388861 Mon Sep 17 00:00:00 2001 From: jvilders <60934195+jvilders@users.noreply.github.com> Date: Sun, 12 Feb 2023 00:05:03 +0100 Subject: [PATCH 1/2] Clarify property order Expand explanation to make it more explicit that integer property names always come before any non-integer property names. Also add a small example to show this. Fix casing of object name Fix indexing Fix name Expand wording on object property order --- 1-js/04-object-basics/01-object/article.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/1-js/04-object-basics/01-object/article.md b/1-js/04-object-basics/01-object/article.md index 0fe5979fa6..c502c22435 100644 --- a/1-js/04-object-basics/01-object/article.md +++ b/1-js/04-object-basics/01-object/article.md @@ -392,7 +392,7 @@ Also, we could use another variable name here instead of `key`. For instance, `" Are objects ordered? In other words, if we loop over an object, do we get all properties in the same order they were added? Can we rely on this? -The short answer is: "ordered in a special fashion": integer properties are sorted, others appear in creation order. The details follow. +The short answer is: "ordered in a special fashion": Integer properties appear first in sorted order, then other properties appear in creation order. The details follow. As an example, let's consider an object with the phone codes: @@ -472,6 +472,21 @@ for (let code in codes) { Now it works as intended. +If an object has both integer properties and non-integer properties, the integer properties will appear first, regardless of creation order. + +```js run +let favoriteGames = {}; +favoriteGames.year = 1996; +favoriteGames.author = "John Smith"; +favoriteGames[3] = "Crash Bandicoot"; +favoriteGames[2] = "Super Mario 64"; +favoriteGames[1] = "Quake"; + +for (let prop in favoriteGames) { + alert( prop ); // 1, 2, 3, year, author +} +``` + ## Summary Objects are associative arrays with several special features. From 8ddbe681c2309f4096dc154c7c221582ceca072f Mon Sep 17 00:00:00 2001 From: Jona Vilders <60934195+jvilders@users.noreply.github.com> Date: Sun, 12 Feb 2023 00:25:14 +0100 Subject: [PATCH 2/2] Change object name in example Makes the integer property names more intuitive. --- 1-js/04-object-basics/01-object/article.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/1-js/04-object-basics/01-object/article.md b/1-js/04-object-basics/01-object/article.md index c502c22435..8f4fbb608e 100644 --- a/1-js/04-object-basics/01-object/article.md +++ b/1-js/04-object-basics/01-object/article.md @@ -475,14 +475,14 @@ Now it works as intended. If an object has both integer properties and non-integer properties, the integer properties will appear first, regardless of creation order. ```js run -let favoriteGames = {}; -favoriteGames.year = 1996; -favoriteGames.author = "John Smith"; -favoriteGames[3] = "Crash Bandicoot"; -favoriteGames[2] = "Super Mario 64"; -favoriteGames[1] = "Quake"; - -for (let prop in favoriteGames) { +let favoriteGamesTopThree = {}; +favoriteGamesTopThree.year = 1996; +favoriteGamesTopThree.author = "John Smith"; +favoriteGamesTopThree[3] = "Crash Bandicoot"; +favoriteGamesTopThree[2] = "Super Mario 64"; +favoriteGamesTopThree[1] = "Quake"; + +for (let prop in favoriteGamesTopThree) { alert( prop ); // 1, 2, 3, year, author } ```