From 37f9093cf541b3fe5bfd36a6682592b8403acbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Mon, 19 Oct 2020 00:13:36 +0200 Subject: [PATCH 1/7] WIP: translated half of article --- .../12-nullish-coalescing-operator/article.md | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) 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..98146898d 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 @@ -1,42 +1,42 @@ -# Nullish coalescing operator '??' +# Operator łączenia wartości null '??' [recent browser="new"] -Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`. +W tym artykule zakładamy, że wyrażenie jest "zdefiniowane", jeżeli nie jest `null` albo `undefined`. -The nullish coalescing operator is written as two question marks `??`. +Operator łączenia wartości null zapisujemy jako dwa znaki zapytania `??`. -The result of `a ?? b` is: -- if `a` is defined, then `a`, -- if `a` isn't defined, then `b`. +Wynikiem `a ?? b` jest: +- jeżeli `a` jest zdefiniowane, to wynikiem jest `a`, +- jeżeli `a` nie jest zdefiniowane, to wynikiem jest `b`. -In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one. +Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. -The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. +Operator łączenia wartości null isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. -We can rewrite `result = a ?? b` using the operators that we already know, like this: +Możemy zapisać `result = a ?? b` używając operatorów, które już znamy: ```js result = (a !== null && a !== undefined) ? a : b; ``` -The common use case for `??` is to provide a default value for a potentially undefined variable. +Typowym przykładem użycia `??` jest dostarczenie domyślnej wartości dla potencjalnie niezdefiniowanej zmiennej. -For example, here we show `Anonymous` if `user` isn't defined: +Dla przykładu, wyświetlamy `Anonimowy`, jeżeli zmienna `user` jest niezdefiniowana: ```js run let user; -alert(user ?? "Anonymous"); // Anonymous +alert(user ?? "Anonimowy"); // Anonimowy ``` -Of course, if `user` had any value except `null/undefined`, then we would see it instead: +Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy oczywiście powinniśmy zobaczyć jej wartość: ```js run let user = "John"; -alert(user ?? "Anonymous"); // John +alert(user ?? "Anonimowy"); // John ``` We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. @@ -77,7 +77,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. -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 `||`. +On the other hand, Operator łączenia wartości null `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`. The important difference between them is that: - `||` returns the first *truthy* value. @@ -153,7 +153,7 @@ alert(x); // 2 ## Summary -- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list. +- Operator łączenia wartości null `??` provides a short way to choose the first "defined" value from a list. It's used to assign default values to variables: From 14c2d048e3dd5dc8fde062e811252a23e74d1cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Thu, 22 Oct 2020 05:49:01 +0200 Subject: [PATCH 2/7] WIP translation part --- .../12-nullish-coalescing-operator/article.md | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) 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 98146898d..410110322 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 @@ -13,7 +13,7 @@ Wynikiem `a ?? b` jest: Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. -Operator łączenia wartości null isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. +Operator łączenia wartości null nie jest całkiem nowy. Jest to po prostu ładna składnia, aby dostać pierwszą zdefiniowaną wartość z dwóch dostępnych. Możemy zapisać `result = a ?? b` używając operatorów, które już znamy: @@ -23,71 +23,71 @@ result = (a !== null && a !== undefined) ? a : b; Typowym przykładem użycia `??` jest dostarczenie domyślnej wartości dla potencjalnie niezdefiniowanej zmiennej. -Dla przykładu, wyświetlamy `Anonimowy`, jeżeli zmienna `user` jest niezdefiniowana: +Dla przykładu, wyświetlamy `Anonim`, jeżeli zmienna `user` jest niezdefiniowana: ```js run let user; -alert(user ?? "Anonimowy"); // Anonimowy +alert(user ?? "Anonim"); // Anonim ``` -Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy oczywiście powinniśmy zobaczyć jej wartość: +Oczywiście, jeżeli zmienna `user` ma inną wartość niż `null/undefined`, wtedy powinniśmy zobaczyć jej wartość: ```js run let user = "John"; -alert(user ?? "Anonimowy"); // John +alert(user ?? "Anonim"); // John ``` -We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. +Możemy również użyć sekwencji `??`, żeby wybrać pierwszą wartość z listy, która jest inna niż `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. +Powiedzmy, że mamy dane użytkownika w zmiennej `firstName`, `lastName` oraz `nickName`. Wszystkie mogą być niezdefiniowane, jeżeli użytkownik zdecyduje się ich nie wprowadzać. -We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are undefined. +Chcielibyśmy wyświetlić nazwę użytkownika użyuwając jednej z tych zmiennych, albo wyświetlić "Anonim", jeżeli wszystkie są niezdefiniowane. -Let's use the `??` operator for that: +Użyjmy do tego operatora `??`: ```js run let firstName = null; let lastName = null; let nickName = "Supercoder"; -// shows the first defined value: +// pokazuje pierwszą zdefiniowaną wartość: *!* -alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder +alert(firstName ?? lastName ?? nickName ?? "Anonim"); // Supercoder */!* ``` -## Comparison with || +## Porównanie z || -The OR `||` operator can be used in the same way as `??`, as it was described in the [previous chapter](info:logical-operators#or-finds-the-first-truthy-value). +Operator OR `||` może być użyty w ten sam sposób co `??`, jak to było opisane w [poprzednim rozdziale](info:logical-operators#or-finds-the-first-truthy-value). -For example, in the code above we could replace `??` with `||` and still get the same result: +Dla przykładu, w kodzie powyżej, możemy zastąpić `??` z `||` i wciąż otrzymać ten sam rezultat: ```js run let firstName = null; let lastName = null; let nickName = "Supercoder"; -// shows the first truthy value: +// pokazuje pierwszą truthy wartość: *!* -alert(firstName || lastName || nickName || "Anonymous"); // Supercoder +alert(firstName || lastName || nickName || "Anonim"); // Supercoder */!* ``` -The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time. +Operator OR `||` istnieje od początku w JavaScript, więc był w ten sposób używany przez developerów od bardzo dawna. -On the other hand, Operator łączenia wartości null `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`. +Z drugiej strony, Operator łączenia wartości null `??` został dodany do JavaScript ostatnio i powodem było to, że ludzie nie byli całkiem zadowoleni z `||`. -The important difference between them is that: -- `||` returns the first *truthy* value. -- `??` returns the first *defined* value. +Ważną różnicą pomiędzy nimi jest: +- `||` zwraca pierwszą *truthy* wartość. +- `??` zwraca pierwszą *defined* wartość. -In other words, `||` doesn't distinguish between `false`, `0`, an empty string `""` and `null/undefined`. They are all the same -- falsy values. If any of these is the first argument of `||`, then we'll get the second argument as the result. +Innymi słowy, `||` nie rozróżnia pomiędzy `false`, `0`, pustym stringiem `""` i `null/undefined`. Wszystkie one są takie same -- falsy wartości. Jeżeli którakolwiek z tych wartości jest pierwszym argumentem w `||`, wtedy otrzymamy drugi argument jako wynik. -In practice though, we may want to use default value only when the variable is `null/undefined`. That is, when the value is really unknown/not set. +W praktyce, możemy chcieć użyć domyślnej wartości tylko wtedy jeżeli zmienna ma wartość `null/undefined`. To znaczy tylko wtedy kiedy wartość naprawdę jest nieznana/nie ustawiona. -For example, consider this: +Na przykład, rozważmy: ```js run let height = 0; @@ -96,47 +96,47 @@ 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 `null/undefined`, and it's not, - - so the result is `height` "as is", that is `0`. +- Wyrażenie `height || 100` sprawdza `height` pod kątem falsy wartości, i jest ona taka. + - w takim razie wynikiem jest drugi argument, `100`. +- Wyrażenie `height ?? 100` sprawdza `height` pod kątem `null/undefined`, i nie jest, + - w takim razie, wynikiem jest `height` "takie jakie jest", zatem `0`. -If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing. +Jeżeli zerowa wysokość jest poprawną wartością, która nie powinna być zastąpiona wartością domyślną, wtedy `??` sprawdzi się doskonale. -## Precedence +## 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 `+`, `*`. +Priorytet operatora `??` jest raczej niski: `5` [tabela MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). Więc `??` jest przetwarzane przed `=` i `?`, ale po większości innych operatorów, jak `+`, `*`. -So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses: +Więc jeżeli chcemy wybrać wartość używając `??` w wyrażeniu z innymi operatorami, rozważ dodanie nawiasów: ```js run let height = null; let width = null; -// important: use parentheses +// ważne: użyj nawiasów let area = (height ?? 100) * (width ?? 50); alert(area); // 5000 ``` -Otherwise, if we omit parentheses, then as `*` has the higher precedence than `??`, it would execute first, leading to incorrect results. +W innym wypadku, jeżeli ominiemy nawiasy, wtedy `*` ma większy priorytet niż `??`, wykona się najpierw, prowadząc do niewłaściwych wyników. ```js -// without parentheses +// bez nawiasów let area = height ?? 100 * width ?? 50; -// ...works the same as this (probably not what we want): +// ...działa tak samo (prawdopodobnie nie tego chcemy): let area = height ?? (100 * width) ?? 50; ``` -### Using ?? with && or || +### Użycie ?? z && albo || -Due to safety reasons, JavaScript forbids using `??` together with `&&` and `||` operators, unless the precedence is explicitly specified with parentheses. +Z powodów bezpieczeństwa, JavaScript zabrania użycia `??` razem z operatorami `&&` i `||`, chyba, że priorytety są zdefiniowane dokładnie z użyciem nawiasów. -The code below triggers a syntax error: +Kod poniżej wywołuje błąd składni: ```js run -let x = 1 && 2 ?? 3; // Syntax error +let x = 1 && 2 ?? 3; // Błąd składni ``` 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 `||`. From 270905c23ade389d70cc489fdc7d7fc1cb6948b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Sat, 24 Oct 2020 21:48:48 +0200 Subject: [PATCH 3/7] All translated - raw --- .../12-nullish-coalescing-operator/article.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 410110322..4b4938491 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 @@ -139,28 +139,28 @@ Kod poniżej wywołuje błąd składni: let x = 1 && 2 ?? 3; // Błąd składni ``` -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 `||`. +Obostrzenia są oczywiście dyskusyjne, ale zostały dodane do specyfikacji języka celem uniknięcia błędów programowania, kiedy ludzie zaczną zmieniać z `??` na `||`. -Use explicit parentheses to work around it: +Używaj dokładnych nawiasów żeby uniknąć problemu: ```js run *!* -let x = (1 && 2) ?? 3; // Works +let x = (1 && 2) ?? 3; // Działa */!* alert(x); // 2 ``` -## Summary +## Podsumowanie -- Operator łączenia wartości null `??` provides a short way to choose the first "defined" value from a list. +- Operator łączenia wartości null `??` dostarcza szybszego sposobu na wybranie pierwszej zdefiniowanej wartości z listy. - It's used to assign default values to variables: + Jest używany do przypisania domyślnej wartości do zmiennej: ```js - // set height=100, if height is null or undefined + // ustaw height=100, jeżeli height jest null lub undefined height = height ?? 100; ``` -- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression. -- It's forbidden to use it with `||` or `&&` without explicit parentheses. +- Operator `??` ma bardzo niski priorytet, tylko trochę wyższy niż `?` i `=`, zatem rozważ dodanie nawiasów w wyrażeniu. +- Zabronione jest użycie z `||` lub `&&` bez użycia nawiasów. From b21e7f2006d16aed36b2f87a6a8e8b19c8cbdf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Sat, 24 Oct 2020 21:58:57 +0200 Subject: [PATCH 4/7] Small corrects --- .../12-nullish-coalescing-operator/article.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 4b4938491..f7f6f909c 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 @@ -7,8 +7,8 @@ W tym artykule zakładamy, że wyrażenie jest "zdefiniowane", jeżeli nie jest Operator łączenia wartości null zapisujemy jako dwa znaki zapytania `??`. Wynikiem `a ?? b` jest: -- jeżeli `a` jest zdefiniowane, to wynikiem jest `a`, -- jeżeli `a` nie jest zdefiniowane, to wynikiem jest `b`. +- `a` jeżeli jest zdefiniowane, +- `b` jeżeli `a` nie jest zdefiniowane. Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. @@ -43,7 +43,7 @@ Możemy również użyć sekwencji `??`, żeby wybrać pierwszą wartość z li Powiedzmy, że mamy dane użytkownika w zmiennej `firstName`, `lastName` oraz `nickName`. Wszystkie mogą być niezdefiniowane, jeżeli użytkownik zdecyduje się ich nie wprowadzać. -Chcielibyśmy wyświetlić nazwę użytkownika użyuwając jednej z tych zmiennych, albo wyświetlić "Anonim", jeżeli wszystkie są niezdefiniowane. +Chcielibyśmy wyświetlić nazwę użytkownika używając jednej z tych zmiennych, albo wyświetlić "Anonim", jeżeli wszystkie są niezdefiniowane. Użyjmy do tego operatora `??`: @@ -81,7 +81,7 @@ Z drugiej strony, Operator łączenia wartości null `??` został dodany do Java Ważną różnicą pomiędzy nimi jest: - `||` zwraca pierwszą *truthy* wartość. -- `??` zwraca pierwszą *defined* wartość. +- `??` zwraca pierwszą *zdefiniowaną* wartość. Innymi słowy, `||` nie rozróżnia pomiędzy `false`, `0`, pustym stringiem `""` i `null/undefined`. Wszystkie one są takie same -- falsy wartości. Jeżeli którakolwiek z tych wartości jest pierwszym argumentem w `||`, wtedy otrzymamy drugi argument jako wynik. @@ -103,7 +103,7 @@ alert(height ?? 100); // 0 Jeżeli zerowa wysokość jest poprawną wartością, która nie powinna być zastąpiona wartością domyślną, wtedy `??` sprawdzi się doskonale. -## Precedence / +## Priorytet / Priorytet operatora `??` jest raczej niski: `5` [tabela MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). Więc `??` jest przetwarzane przed `=` i `?`, ale po większości innych operatorów, jak `+`, `*`. @@ -141,7 +141,7 @@ let x = 1 && 2 ?? 3; // Błąd składni Obostrzenia są oczywiście dyskusyjne, ale zostały dodane do specyfikacji języka celem uniknięcia błędów programowania, kiedy ludzie zaczną zmieniać z `??` na `||`. -Używaj dokładnych nawiasów żeby uniknąć problemu: +Używaj nawiasów żeby uniknąć problemu: ```js run *!* @@ -158,7 +158,7 @@ alert(x); // 2 Jest używany do przypisania domyślnej wartości do zmiennej: ```js - // ustaw height=100, jeżeli height jest null lub undefined + // ustaw height=100, jeżeli height jest null lub undefined height = height ?? 100; ``` From f8d4b3ae1887080b2d9dc015e9581b7ab139d5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Sat, 24 Oct 2020 22:01:26 +0200 Subject: [PATCH 5/7] Remove misspell --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f7f6f909c..e7351180a 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 @@ -103,7 +103,7 @@ alert(height ?? 100); // 0 Jeżeli zerowa wysokość jest poprawną wartością, która nie powinna być zastąpiona wartością domyślną, wtedy `??` sprawdzi się doskonale. -## Priorytet / +## Priorytet Priorytet operatora `??` jest raczej niski: `5` [tabela MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). Więc `??` jest przetwarzane przed `=` i `?`, ale po większości innych operatorów, jak `+`, `*`. From 7d65cc7c0e52fefd29a3f2773cd8801fb77da0a0 Mon Sep 17 00:00:00 2001 From: sculpt0r Date: Fri, 12 Aug 2022 13:39:37 +0200 Subject: [PATCH 6/7] Correct according to review --- .../12-nullish-coalescing-operator/article.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 e7351180a..8157eca17 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 @@ -1,4 +1,4 @@ -# Operator łączenia wartości null '??' +# Operator null'owego scalania '??' [recent browser="new"] @@ -11,7 +11,7 @@ Wynikiem `a ?? b` jest: - `b` jeżeli `a` nie jest zdefiniowane. -Innymi słowy, `??` zwraca pierwszy argument jeżeli jego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. +Innymi słowy, `??` zwraca pierwszy argument, którego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. Operator łączenia wartości null nie jest całkiem nowy. Jest to po prostu ładna składnia, aby dostać pierwszą zdefiniowaną wartość z dwóch dostępnych. @@ -41,7 +41,7 @@ alert(user ?? "Anonim"); // John Możemy również użyć sekwencji `??`, żeby wybrać pierwszą wartość z listy, która jest inna niż `null/undefined`. -Powiedzmy, że mamy dane użytkownika w zmiennej `firstName`, `lastName` oraz `nickName`. Wszystkie mogą być niezdefiniowane, jeżeli użytkownik zdecyduje się ich nie wprowadzać. +Powiedzmy, że mamy dane użytkownika w zmiennych `firstName`, `lastName` oraz `nickName`. Wszystkie mogą być niezdefiniowane, jeżeli użytkownik zdecyduje się ich nie wprowadzać. Chcielibyśmy wyświetlić nazwę użytkownika używając jednej z tych zmiennych, albo wyświetlić "Anonim", jeżeli wszystkie są niezdefiniowane. @@ -85,7 +85,7 @@ Ważną różnicą pomiędzy nimi jest: Innymi słowy, `||` nie rozróżnia pomiędzy `false`, `0`, pustym stringiem `""` i `null/undefined`. Wszystkie one są takie same -- falsy wartości. Jeżeli którakolwiek z tych wartości jest pierwszym argumentem w `||`, wtedy otrzymamy drugi argument jako wynik. -W praktyce, możemy chcieć użyć domyślnej wartości tylko wtedy jeżeli zmienna ma wartość `null/undefined`. To znaczy tylko wtedy kiedy wartość naprawdę jest nieznana/nie ustawiona. +W praktyce jednak, możemy chcieć użyć domyślnej wartości tylko wtedy jeżeli zmienna ma wartość `null/undefined`. To znaczy tylko wtedy kiedy wartość naprawdę jest nieznana/nie ustawiona. Na przykład, rozważmy: @@ -96,9 +96,9 @@ alert(height || 100); // 100 alert(height ?? 100); // 0 ``` -- Wyrażenie `height || 100` sprawdza `height` pod kątem falsy wartości, i jest ona taka. +- Wyrażenie `height || 100` sprawdza `height` pod kątem falsy wartości, i tak też właśnie jest. - w takim razie wynikiem jest drugi argument, `100`. -- Wyrażenie `height ?? 100` sprawdza `height` pod kątem `null/undefined`, i nie jest, +- Wyrażenie `height ?? 100` sprawdza `height` pod kątem `null/undefined`, a zmienna `height` nie jest żadną z tych wartości, - w takim razie, wynikiem jest `height` "takie jakie jest", zatem `0`. Jeżeli zerowa wysokość jest poprawną wartością, która nie powinna być zastąpiona wartością domyślną, wtedy `??` sprawdzi się doskonale. @@ -119,7 +119,7 @@ let area = (height ?? 100) * (width ?? 50); alert(area); // 5000 ``` -W innym wypadku, jeżeli ominiemy nawiasy, wtedy `*` ma większy priorytet niż `??`, wykona się najpierw, prowadząc do niewłaściwych wyników. +W innym wypadku, jeżeli ominiemy nawiasy, wtedy `*` ma większy priorytet niż `??`, więc wykona się najpierw, prowadząc do niewłaściwych wyników. ```js // bez nawiasów @@ -136,7 +136,7 @@ Z powodów bezpieczeństwa, JavaScript zabrania użycia `??` razem z operatorami Kod poniżej wywołuje błąd składni: ```js run -let x = 1 && 2 ?? 3; // Błąd składni +let x = 1 && 2 ?? 3; // Błąd składni (eng. syntax error) ``` Obostrzenia są oczywiście dyskusyjne, ale zostały dodane do specyfikacji języka celem uniknięcia błędów programowania, kiedy ludzie zaczną zmieniać z `??` na `||`. @@ -153,7 +153,7 @@ alert(x); // 2 ## Podsumowanie -- Operator łączenia wartości null `??` dostarcza szybszego sposobu na wybranie pierwszej zdefiniowanej wartości z listy. +- Operator null'owego scalania `??` dostarcza szybszego sposobu na wybranie pierwszej zdefiniowanej wartości z listy. Jest używany do przypisania domyślnej wartości do zmiennej: From 817815b9dfeb2336ebb7cf3a387ba83845075512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kalemba?= Date: Thu, 13 Oct 2022 18:56:48 +0200 Subject: [PATCH 7/7] Correct c --- .../12-nullish-coalescing-operator/article.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 8157eca17..676849f86 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 @@ -4,7 +4,7 @@ W tym artykule zakładamy, że wyrażenie jest "zdefiniowane", jeżeli nie jest `null` albo `undefined`. -Operator łączenia wartości null zapisujemy jako dwa znaki zapytania `??`. +Operator null'owego scalania zapisujemy jako dwa znaki zapytania `??`. Wynikiem `a ?? b` jest: - `a` jeżeli jest zdefiniowane, @@ -13,7 +13,7 @@ Wynikiem `a ?? b` jest: Innymi słowy, `??` zwraca pierwszy argument, którego wartość jest inna niż `null/undefined`. W przeciwnym razie, zwraca drugi argument. -Operator łączenia wartości null nie jest całkiem nowy. Jest to po prostu ładna składnia, aby dostać pierwszą zdefiniowaną wartość z dwóch dostępnych. +Operator null'owego scalania nie jest całkiem nowy. Jest to po prostu ładna składnia, aby dostać pierwszą zdefiniowaną wartość z dwóch dostępnych. Możemy zapisać `result = a ?? b` używając operatorów, które już znamy: @@ -77,7 +77,7 @@ alert(firstName || lastName || nickName || "Anonim"); // Supercoder Operator OR `||` istnieje od początku w JavaScript, więc był w ten sposób używany przez developerów od bardzo dawna. -Z drugiej strony, Operator łączenia wartości null `??` został dodany do JavaScript ostatnio i powodem było to, że ludzie nie byli całkiem zadowoleni z `||`. +Z drugiej strony, Operator null'owego scalania `??` został dodany do JavaScript ostatnio i powodem było to, że ludzie nie byli całkiem zadowoleni z `||`. Ważną różnicą pomiędzy nimi jest: - `||` zwraca pierwszą *truthy* wartość. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy