From aef2a02f42b0e7987bbbfd52872dd1ee896a08f4 Mon Sep 17 00:00:00 2001 From: Junkang Li <17224946+lijunkang12@users.noreply.github.com> Date: Sat, 23 Jan 2021 16:42:55 +0100 Subject: [PATCH 01/15] Update task.md To be coherent with the solution and the picture below. --- 1-js/02-first-steps/10-ifelse/2-check-standard/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/10-ifelse/2-check-standard/task.md b/1-js/02-first-steps/10-ifelse/2-check-standard/task.md index a4d943245..4305584fa 100644 --- a/1-js/02-first-steps/10-ifelse/2-check-standard/task.md +++ b/1-js/02-first-steps/10-ifelse/2-check-standard/task.md @@ -6,7 +6,7 @@ importance: 2 Using the `if..else` construct, write the code which asks: 'What is the "official" name of JavaScript?' -If the visitor enters "ECMAScript", then output "Right!", otherwise -- output: "Didn't know? ECMAScript!" +If the visitor enters "ECMAScript", then output "Right!", otherwise -- output: "You don't know? ECMAScript!" ![](ifelse_task2.svg) From 6975a019415d42f5b3693d7cb42f157af2efdbb1 Mon Sep 17 00:00:00 2001 From: Ali Aghdam Date: Mon, 25 Jan 2021 14:54:10 +0330 Subject: [PATCH 02/15] fix undefined to pass test --- .../6-max-salary/_js.view/solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/05-data-types/10-destructuring-assignment/6-max-salary/_js.view/solution.js b/1-js/05-data-types/10-destructuring-assignment/6-max-salary/_js.view/solution.js index 03ddb1528..6538af42b 100644 --- a/1-js/05-data-types/10-destructuring-assignment/6-max-salary/_js.view/solution.js +++ b/1-js/05-data-types/10-destructuring-assignment/6-max-salary/_js.view/solution.js @@ -4,7 +4,7 @@ function topSalary(salaries) { let maxName = null; for(const [name, salary] of Object.entries(salaries)) { - if (max < salary) { + if (maxSalary < salary) { maxSalary = salary; maxName = name; } From 936bea24bc798946b44154e2cfbc9a5b537c3de6 Mon Sep 17 00:00:00 2001 From: fibretothepremises <65995595+fibretothepremises@users.noreply.github.com> Date: Wed, 27 Jan 2021 17:39:35 +1100 Subject: [PATCH 03/15] fix word fix word "when" to "than". --- 1-js/05-data-types/10-destructuring-assignment/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/05-data-types/10-destructuring-assignment/article.md b/1-js/05-data-types/10-destructuring-assignment/article.md index e9077a595..fb9346aa2 100644 --- a/1-js/05-data-types/10-destructuring-assignment/article.md +++ b/1-js/05-data-types/10-destructuring-assignment/article.md @@ -151,7 +151,7 @@ We can swap more than two variables this way. ### The rest '...' -Usually, if the array is longer when the list at the left, the "extra" items are omitted. +Usually, if the array is longer than the list at the left, the "extra" items are omitted. For example, here only two items are taken, and the rest is just ignored: From 1246f79ca24fa5e75f0e97da441b0260f34642b2 Mon Sep 17 00:00:00 2001 From: Jordan Cates <62562361+helloimjordan@users.noreply.github.com> Date: Wed, 27 Jan 2021 17:22:26 -0800 Subject: [PATCH 04/15] Fix article typo remove unnecessary 'in' --- 1-js/01-getting-started/2-manuals-specifications/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/01-getting-started/2-manuals-specifications/article.md b/1-js/01-getting-started/2-manuals-specifications/article.md index 76eb1a811..2824232bb 100644 --- a/1-js/01-getting-started/2-manuals-specifications/article.md +++ b/1-js/01-getting-started/2-manuals-specifications/article.md @@ -13,7 +13,7 @@ A new specification version is released every year. In-between these releases, t To read about new bleeding-edge features, including those that are "almost standard" (so-called "stage 3"), see proposals at . -Also, if you're in developing for the browser, then there are other specifications covered in the [second part](info:browser-environment) of the tutorial. +Also, if you're developing for the browser, then there are other specifications covered in the [second part](info:browser-environment) of the tutorial. ## Manuals From 3f8ab2426d305af6da470f29b5ce28c39784aa93 Mon Sep 17 00:00:00 2001 From: Pawel Date: Fri, 29 Jan 2021 12:32:39 +0100 Subject: [PATCH 05/15] Fix typo in "liseners" on line 21 --- 5-network/04-fetch-abort/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/5-network/04-fetch-abort/article.md b/5-network/04-fetch-abort/article.md index 14e8d13cb..af66c37cc 100644 --- a/5-network/04-fetch-abort/article.md +++ b/5-network/04-fetch-abort/article.md @@ -18,7 +18,7 @@ let controller = new AbortController(); A controller is an extremely simple object. - It has a single method `abort()`, -- And a single property `signal` that allows to set event liseners on it. +- And a single property `signal` that allows to set event listeners on it. When `abort()` is called: - `controller.signal` emits the `"abort"` event. From 526b216871a4e4b7309cab2e644d25d1eb704022 Mon Sep 17 00:00:00 2001 From: seamissu <77573461+seamissu@users.noreply.github.com> Date: Sat, 30 Jan 2021 04:26:38 +1100 Subject: [PATCH 06/15] fix typo: missing semicolon after let ladder = { } --- .../04-object-methods/8-chain-calls/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md b/1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md index 2b47873fc..ab4e37340 100644 --- a/1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md +++ b/1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md @@ -21,7 +21,7 @@ let ladder = { return this; */!* } -} +}; ladder.up().up().down().up().down().showStep(); // 1 ``` From 82e4e212f483b03d1f0648144d066a934e6ddb92 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Mon, 1 Feb 2021 11:11:35 +0300 Subject: [PATCH 07/15] there are four logical operators in JS https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator --- 1-js/02-first-steps/11-logical-operators/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index caa8cdfaf..0306e43fa 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,6 +1,6 @@ # Logical operators -There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT). +There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), '??' (Nullish Coalescing). Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. From 21d444fcb5ce0cf44bfbcfb2cf32020bfdcee986 Mon Sep 17 00:00:00 2001 From: Filip Pyda Date: Mon, 1 Feb 2021 09:32:46 +0100 Subject: [PATCH 08/15] Add will to modyfying document task 1 sentence --- .../07-modifying-document/1-createtextnode-vs-innerhtml/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ui/1-document/07-modifying-document/1-createtextnode-vs-innerhtml/task.md b/2-ui/1-document/07-modifying-document/1-createtextnode-vs-innerhtml/task.md index e127bc0ef..40c75dff3 100644 --- a/2-ui/1-document/07-modifying-document/1-createtextnode-vs-innerhtml/task.md +++ b/2-ui/1-document/07-modifying-document/1-createtextnode-vs-innerhtml/task.md @@ -6,7 +6,7 @@ importance: 5 We have an empty DOM element `elem` and a string `text`. -Which of these 3 commands do exactly the same? +Which of these 3 commands will do exactly the same? 1. `elem.append(document.createTextNode(text))` 2. `elem.innerHTML = text` From 4df24f3a083b0a58117c6b913a1b7a89d917faa4 Mon Sep 17 00:00:00 2001 From: Muhammed Zakir <8190126+MuhammedZakir@users.noreply.github.com> Date: Mon, 1 Feb 2021 21:42:48 +0530 Subject: [PATCH 09/15] Add missing semicolon in Arrow Functions (1-6-12) --- 1-js/06-advanced-functions/12-arrow-functions/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/06-advanced-functions/12-arrow-functions/article.md b/1-js/06-advanced-functions/12-arrow-functions/article.md index f5caeaece..8730277ad 100644 --- a/1-js/06-advanced-functions/12-arrow-functions/article.md +++ b/1-js/06-advanced-functions/12-arrow-functions/article.md @@ -52,7 +52,7 @@ let group = { *!* this.students.forEach(function(student) { // Error: Cannot read property 'title' of undefined - alert(this.title + ': ' + student) + alert(this.title + ': ' + student); }); */!* } @@ -87,7 +87,7 @@ For instance, `defer(f, ms)` gets a function and returns a wrapper around it tha ```js run function defer(f, ms) { return function() { - setTimeout(() => f.apply(this, arguments), ms) + setTimeout(() => f.apply(this, arguments), ms); }; } From 18534d3abd0f27f09d16c08cfda26ef6e6b836fe Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 1 Feb 2021 18:07:49 +0000 Subject: [PATCH 10/15] Clearer CachingDecorator comments - comments are in pairs. The first of the pair talks about what happened "caching" and the second pair talks about what was alerted "the same". This is confusing. - changed comment so both lines tell the reader what happened and what result was returned. --- .../09-call-apply-decorators/article.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1-js/06-advanced-functions/09-call-apply-decorators/article.md b/1-js/06-advanced-functions/09-call-apply-decorators/article.md index d0dda4df1..acba0965c 100644 --- a/1-js/06-advanced-functions/09-call-apply-decorators/article.md +++ b/1-js/06-advanced-functions/09-call-apply-decorators/article.md @@ -36,11 +36,11 @@ function cachingDecorator(func) { slow = cachingDecorator(slow); -alert( slow(1) ); // slow(1) is cached -alert( "Again: " + slow(1) ); // the same +alert( slow(1) ); // slow(1) is cached and the result returned +alert( "Again: " + slow(1) ); // slow(1) result returned from cache -alert( slow(2) ); // slow(2) is cached -alert( "Again: " + slow(2) ); // the same as the previous line +alert( slow(2) ); // slow(2) is cached and the result returned +alert( "Again: " + slow(2) ); // slow(2) result returned from cache ``` In the code above `cachingDecorator` is a *decorator*: a special function that takes another function and alters its behavior. From 96e95bb3d1d69ce456a4f879202c24ae0f7e8dab Mon Sep 17 00:00:00 2001 From: Aniket Samant <43705966+asamant@users.noreply.github.com> Date: Tue, 2 Feb 2021 03:47:59 +0530 Subject: [PATCH 11/15] Update article.md A minor addition. (...where such thing doesn't happen. --> where such a thing doesn't happen.) --- 1-js/06-advanced-functions/05-global-object/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/06-advanced-functions/05-global-object/article.md b/1-js/06-advanced-functions/05-global-object/article.md index ae4883c48..40131e339 100644 --- a/1-js/06-advanced-functions/05-global-object/article.md +++ b/1-js/06-advanced-functions/05-global-object/article.md @@ -27,7 +27,7 @@ alert(window.gVar); // 5 (became a property of the global object) The same effect have function declarations (statements with `function` keyword in the main code flow, not function expressions). -Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such thing doesn't happen. +Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such a thing doesn't happen. If we used `let` instead, such thing wouldn't happen: From 005204e107ec35086db377552f1c92f0758273f2 Mon Sep 17 00:00:00 2001 From: Mustafa Kemal Tuna <12192118+lumosmind@users.noreply.github.com> Date: Tue, 2 Feb 2021 07:03:39 +0300 Subject: [PATCH 12/15] conflict with "rest syntax" I think argN represents count of arbitrary parameters. But this syntax looks like "rest syntax". It could be more understandable to change it like this. --- 1-js/02-first-steps/17-arrow-functions-basics/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/02-first-steps/17-arrow-functions-basics/article.md b/1-js/02-first-steps/17-arrow-functions-basics/article.md index e0fb5bda5..1b6da9bfe 100644 --- a/1-js/02-first-steps/17-arrow-functions-basics/article.md +++ b/1-js/02-first-steps/17-arrow-functions-basics/article.md @@ -5,7 +5,7 @@ There's another very simple and concise syntax for creating functions, that's of It's called "arrow functions", because it looks like this: ```js -let func = (arg1, arg2, ...argN) => expression +let func = (arg1, arg2, ..., argN) => expression ``` ...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result. @@ -13,7 +13,7 @@ let func = (arg1, arg2, ...argN) => expression In other words, it's the shorter version of: ```js -let func = function(arg1, arg2, ...argN) { +let func = function(arg1, arg2, ..., argN) { return expression; }; ``` From 0806399c51e5182ef5c98aa35004bc6f62f659ad Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Tue, 2 Feb 2021 10:37:28 +0300 Subject: [PATCH 13/15] minor fixes --- .../11-logical-operators/article.md | 2 +- 1-js/13-modules/02-import-export/article.md | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 0306e43fa..19682d722 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,4 +1,4 @@ -# Logical operators +# Logical operators: ||, && and ! There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), '??' (Nullish Coalescing). diff --git a/1-js/13-modules/02-import-export/article.md b/1-js/13-modules/02-import-export/article.md index 497205f42..7f24d29ce 100644 --- a/1-js/13-modules/02-import-export/article.md +++ b/1-js/13-modules/02-import-export/article.md @@ -337,13 +337,19 @@ auth/ ... ``` -We'd like to expose the package functionality via a single entry point, the "main file" `auth/index.js`, to be used like this: +We'd like to expose the package functionality via a single entry point. + +In other words, a person who would like to use our package, should import only from the "main file" `auth/index.js`. + +Like this: ```js import {login, logout} from 'auth/index.js' ``` -The idea is that outsiders, developers who use our package, should not meddle with its internal structure, search for files inside our package folder. We export only what's necessary in `auth/index.js` and keep the rest hidden from prying eyes. +The "main file", `auth/index.js` exports all the functionality that we'd like to provide in our package. + +The idea is that outsiders, other programmers who use our package, should not meddle with its internal structure, search for files inside our package folder. We export only what's necessary in `auth/index.js` and keep the rest hidden from prying eyes. As the actual exported functionality is scattered among the package, we can import it into `auth/index.js` and export from it: @@ -366,14 +372,16 @@ The syntax `export ... from ...` is just a shorter notation for such import-expo ```js // 📁 auth/index.js -// import login/logout and immediately export them +// re-export login/logout export {login, logout} from './helpers.js'; -// import default as User and export it +// re-export the default export as User export {default as User} from './user.js'; ... ``` +The notable difference of `export ... from` compared to `import/export` is that re-exported modules aren't available in the current file. So inside the above example of `auth/index.js` we can't use re-exported `login/logout` functions. + ### Re-exporting the default export The default export needs separate handling when re-exporting. From 3fa4c32e1d3944271f44c745529b320a38c242d2 Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Tue, 2 Feb 2021 11:43:35 +0300 Subject: [PATCH 14/15] minor fixes --- .../solution.md | 1 - .../3-primitive-conversions-questions/task.md | 1 - .../11-logical-operators/article.md | 4 +-- .../12-nullish-coalescing-operator/article.md | 33 ++++++++++--------- .../02-object-copy/article.md | 2 ++ .../07-optional-chaining/article.md | 4 +-- .../02-rest-parameters-spread/article.md | 10 ++++-- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md b/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md index 9a8411fbe..dfd061cb6 100644 --- a/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md +++ b/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md @@ -9,7 +9,6 @@ true + false = 1 "$" + 4 + 5 = "$45" "4" - 2 = 2 "4px" - 2 = NaN -7 / 0 = Infinity " -9 " + 5 = " -9 5" // (3) " -9 " - 5 = -14 // (4) null + 1 = 1 // (5) diff --git a/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/task.md b/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/task.md index 930c71514..068420c7d 100644 --- a/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/task.md +++ b/1-js/02-first-steps/08-operators/3-primitive-conversions-questions/task.md @@ -16,7 +16,6 @@ true + false "$" + 4 + 5 "4" - 2 "4px" - 2 -7 / 0 " -9 " + 5 " -9 " - 5 null + 1 diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 19682d722..0842bf339 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,6 +1,6 @@ -# Logical operators: ||, && and ! +# Logical operators -There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), '??' (Nullish Coalescing). +There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), `??` (Nullish Coalescing). Here we cover the first three, the `??` operator is in the next article. Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 55e0c2067..b84dff892 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -2,15 +2,14 @@ [recent browser="new"] -Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`. - The nullish coalescing operator is written as two question marks `??`. +As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither `null` nor `undefined`. + The result of `a ?? b` is: - if `a` is defined, then `a`, - if `a` isn't defined, then `b`. - In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one. The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. @@ -21,29 +20,31 @@ We can rewrite `result = a ?? b` using the operators that we already know, like result = (a !== null && a !== undefined) ? a : b; ``` +Now it should be absolutely clear what `??` does. Let's see where it helps. + The common use case for `??` is to provide a default value for a potentially undefined variable. -For example, here we show `Anonymous` if `user` isn't defined: +For example, here we show `user` if defined, otherwise `Anonymous`: ```js run let user; -alert(user ?? "Anonymous"); // Anonymous +alert(user ?? "Anonymous"); // Anonymous (user not defined) ``` -Of course, if `user` had any value except `null/undefined`, then we would see it instead: +Here's the example with `user` assigned to a name: ```js run let user = "John"; -alert(user ?? "Anonymous"); // John +alert(user ?? "Anonymous"); // John (user defined) ``` We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. -Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be undefined, if the user decided not to enter a value. +Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to enter a value. -We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are undefined. +We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined. Let's use the `??` operator for that: @@ -75,7 +76,7 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder */!* ``` -The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time. +Historically, the OR `||` operator was there first. It exists since the beginning of JavaScript, so developers were using it for such purposes for a long time. On the other hand, the nullish coalescing operator `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`. @@ -96,16 +97,18 @@ alert(height || 100); // 100 alert(height ?? 100); // 0 ``` -- The `height || 100` checks `height` for being a falsy value, and it really is. - - so the result is the second argument, `100`. +- The `height || 100` checks `height` for being a falsy value, and it's `0`, falsy indeed. + - so the result of `||` is the second argument, `100`. - The `height ?? 100` checks `height` for being `null/undefined`, and it's not, - so the result is `height` "as is", that is `0`. -If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing. +In practice, the zero height is often a valid value, that shouldn't be replaced with the default. So `??` does just the right thing. ## Precedence -The precedence of the `??` operator is rather low: `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). So `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`. +The precedence of the `??` operator is about the same as `||`, just a bit lower. It equals `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), while `||` is `6`. + +That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`. So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses: @@ -139,7 +142,7 @@ The code below triggers a syntax error: let x = 1 && 2 ?? 3; // Syntax error ``` -The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch to `??` from `||`. +The limitation is surely debatable, it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch from `||` to `??`. Use explicit parentheses to work around it: diff --git a/1-js/04-object-basics/02-object-copy/article.md b/1-js/04-object-basics/02-object-copy/article.md index bf646a382..52af1bdc5 100644 --- a/1-js/04-object-basics/02-object-copy/article.md +++ b/1-js/04-object-basics/02-object-copy/article.md @@ -186,6 +186,8 @@ let clone = Object.assign({}, user); It copies all properties of `user` into the empty object and returns it. +There are also other methods of cloning an object, e.g. using the [spread operator](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial. + ## Nested cloning Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them? diff --git a/1-js/04-object-basics/07-optional-chaining/article.md b/1-js/04-object-basics/07-optional-chaining/article.md index 0f1ea9a86..9591dcd6c 100644 --- a/1-js/04-object-basics/07-optional-chaining/article.md +++ b/1-js/04-object-basics/07-optional-chaining/article.md @@ -166,9 +166,9 @@ userGuest.admin?.(); // nothing (no such method) */!* ``` -Here, in both lines we first use the dot (`user1.admin`) to get `admin` property, because the user object must exist, so it's safe read from it. +Here, in both lines we first use the dot (`userAdmin.admin`) to get `admin` property, because we assume that the user object exists, so it's safe read from it. -Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `user1`). Otherwise (for `user2`) the evaluation stops without errors. +Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `userAdmin`). Otherwise (for `userGuest`) the evaluation stops without errors. The `?.[]` syntax also works, if we'd like to use brackets `[]` to access properties instead of dot `.`. Similar to previous cases, it allows to safely read a property from an object that may not exist. diff --git a/1-js/06-advanced-functions/02-rest-parameters-spread/article.md b/1-js/06-advanced-functions/02-rest-parameters-spread/article.md index 1f139d7a4..c63fe70cd 100644 --- a/1-js/06-advanced-functions/02-rest-parameters-spread/article.md +++ b/1-js/06-advanced-functions/02-rest-parameters-spread/article.md @@ -225,7 +225,7 @@ But there's a subtle difference between `Array.from(obj)` and `[...obj]`: So, for the task of turning something into an array, `Array.from` tends to be more universal. -## Get a new copy of an array/object +## Copy an array/object Remember when we talked about `Object.assign()` [in the past](info:object-copy#cloning-and-merging-object-assign)? @@ -233,8 +233,11 @@ It is possible to do the same thing with the spread syntax. ```js run let arr = [1, 2, 3]; + +*!* let arrCopy = [...arr]; // spread the array into a list of parameters // then put the result into a new array +*/!* // do the arrays have the same contents? alert(JSON.stringify(arr) === JSON.stringify(arrCopy)); // true @@ -252,8 +255,11 @@ Note that it is possible to do the same thing to make a copy of an object: ```js run let obj = { a: 1, b: 2, c: 3 }; + +*!* let objCopy = { ...obj }; // spread the object into a list of parameters // then return the result in a new object +*/!* // do the objects have the same contents? alert(JSON.stringify(obj) === JSON.stringify(objCopy)); // true @@ -267,7 +273,7 @@ alert(JSON.stringify(obj)); // {"a":1,"b":2,"c":3,"d":4} alert(JSON.stringify(objCopy)); // {"a":1,"b":2,"c":3} ``` -This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj);` or for an array `let arrCopy = Object.assign([], arr);` so we prefer to use it whenever we can. +This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj)` or for an array `let arrCopy = Object.assign([], arr)` so we prefer to use it whenever we can. ## Summary From 7533c719fbf62ba57188d6d51fe4c038b282bd0c Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Tue, 2 Feb 2021 16:56:39 +0300 Subject: [PATCH 15/15] minor fixes --- 1-js/11-async/02-promise-basics/article.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-js/11-async/02-promise-basics/article.md b/1-js/11-async/02-promise-basics/article.md index cbec7c46b..a8fb3d9e7 100644 --- a/1-js/11-async/02-promise-basics/article.md +++ b/1-js/11-async/02-promise-basics/article.md @@ -1,10 +1,10 @@ # Promise -Imagine that you're a top singer, and fans ask day and night for your upcoming single. +Imagine that you're a top singer, and fans ask day and night for your upcoming song. To get some relief, you promise to send it to them when it's published. You give your fans a list. They can fill in their email addresses, so that when the song becomes available, all subscribed parties instantly receive it. And even if something goes very wrong, say, a fire in the studio, so that you can't publish the song, they will still be notified. -Everyone is happy: you, because the people don't crowd you anymore, and fans, because they won't miss the single. +Everyone is happy: you, because the people don't crowd you anymore, and fans, because they won't miss the song. This is a real-life analogy for things we often have in programming: