|
1 |
| -# Code structure |
| 1 | +# Struktura kodu |
2 | 2 |
|
3 |
| -The first thing we'll study is the building blocks of code. |
| 3 | +Na początek skupimy się na częściach składowych pisanego przez nas kodu. |
4 | 4 |
|
5 |
| -## Statements |
| 5 | +## Instrukcje |
6 | 6 |
|
7 |
| -Statements are syntax constructs and commands that perform actions. |
| 7 | +Instrukcje są zapisem składni i komend, które wykonują określone działania. |
8 | 8 |
|
9 |
| -We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!". |
| 9 | +Poznaliśmy już instrukcję `alert('Witaj, świecie!')`, która wyświetlała komunikat "Witaj, świecie!". |
10 | 10 |
|
11 |
| -We can have as many statements in our code as we want. Statements can be separated with a semicolon. |
| 11 | +W naszym kodzie możemy mieć tyle instrukcji, ile chcemy. Każdą z nich można oddzielić średnikiem. |
12 | 12 |
|
13 |
| -For example, here we split "Hello World" into two alerts: |
| 13 | +Na przykład, jeśli chcemy dwukrotnie wyświetlić komunikat, kod będzie wyglądał następująco: |
14 | 14 |
|
15 | 15 | ```js run no-beautify
|
16 |
| -alert('Hello'); alert('World'); |
| 16 | +alert("Witaj"); |
| 17 | +alert("świecie"); |
17 | 18 | ```
|
18 | 19 |
|
19 |
| -Usually, statements are written on separate lines to make the code more readable: |
| 20 | +Instrukcje piszemy zazwyczaj w osobnych wierszach. Dzięki temu kod staje się czytelniejszy: |
20 | 21 |
|
21 | 22 | ```js run no-beautify
|
22 |
| -alert('Hello'); |
23 |
| -alert('World'); |
| 23 | +alert("Witaj"); |
| 24 | +alert("świecie"); |
24 | 25 | ```
|
25 | 26 |
|
26 |
| -## Semicolons [#semicolon] |
| 27 | +## Średniki [#semicolon] |
27 | 28 |
|
28 |
| -A semicolon may be omitted in most cases when a line break exists. |
| 29 | +Średnik w większości przypadków można pominąć, o ile kod podzieliliśmy na osobne wiersze. |
29 | 30 |
|
30 |
| -This would also work: |
| 31 | +To także zadziała: |
31 | 32 |
|
32 | 33 | ```js run no-beautify
|
33 |
| -alert('Hello') |
34 |
| -alert('World') |
| 34 | +alert("Hello"); |
| 35 | +alert("World"); |
35 | 36 | ```
|
36 | 37 |
|
37 |
| -Here, JavaScript interprets the line break as an "implicit" semicolon. This is called an [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
| 38 | +W tym przykładzie JavaScript interpretuje każdy podział linii jako "niejawny" średnik. Mechanizm ten nazywa się [automatycznym wstawianiem średnika](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
38 | 39 |
|
39 |
| -**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!** |
| 40 | +**W większości przypadków nowa linia jest równoznaczna z postawieniem średnika. Co nie oznacza, że tak jest zawsze!** |
40 | 41 |
|
41 |
| -There are cases when a newline does not mean a semicolon. For example: |
| 42 | +Istnieje kilka sytuacji, w których nowa linia nie oznacza, że powinien tam znaleźć się średnik. Na przykład: |
42 | 43 |
|
43 | 44 | ```js run no-beautify
|
44 |
| -alert(3 + |
45 |
| -1 |
46 |
| -+ 2); |
| 45 | +alert(3 + 1 + 2); |
47 | 46 | ```
|
48 | 47 |
|
49 |
| -The code outputs `6` because JavaScript does not insert semicolons here. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an "incomplete expression", so the semicolon is not required. And in this case that works as intended. |
| 48 | +Kod zwróci `6`, ponieważ JavaScript nie wstawi średnika na końcu linii. Wydaje się oczywistym, że jeśli linia kończy się wyrażeniem, na przykład plusem `"+"`, to mamy do czynienia z "niepełnym wyrażeniem" i średnik nie jest wymagany. I w tym przypadku działa to zgodnie z oczekiwaniami. |
50 | 49 |
|
51 |
| -**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** |
| 50 | +**Są jednak sytuacje, w których JavaScript błędnie zakłada, gdzie średnik jest rzeczywiście potrzebny.** |
52 | 51 |
|
53 |
| -Errors which occur in such cases are quite hard to find and fix. |
| 52 | +Błąd, który się pojawi w takim przypadku, jest trudny do wykrycia i naprawienia. |
54 | 53 |
|
55 |
| -````smart header="An example of an error" |
56 |
| -If you're curious to see a concrete example of such an error, check this code out: |
| 54 | +````smart header="Przykład błędu" |
| 55 | +Jeśli ciekawi cię konkretny przykład takiego błędu, uruchom poniższy kod: |
57 | 56 |
|
58 | 57 | ```js run
|
59 | 58 | [1, 2].forEach(alert)
|
60 | 59 | ```
|
61 | 60 |
|
62 |
| -No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`. |
| 61 | +Nie musisz się zastanawiać, co oznaczają te nawiasy kwadratowe `[]` ani czym jest `forEach`. Będzie o tym później. Na tę chwilę musisz wiedzieć, że rezultatem będzie wyświetlenie najpierw `1`, a później `2`. |
63 | 62 |
|
64 |
| -Now, let's add an `alert` before the code and *not* finish it with a semicolon: |
| 63 | +Teraz dodaj `alert` przed kodem i *nie* dodawaj średnika na końcu linii: |
65 | 64 |
|
66 | 65 | ```js run no-beautify
|
67 |
| -alert("There will be an error") |
| 66 | +alert("Tutaj będzie błąd") |
68 | 67 |
|
69 | 68 | [1, 2].forEach(alert)
|
70 | 69 | ```
|
71 | 70 |
|
72 |
| -Now if we run the code, only the first `alert` is shown and then we have an error! |
| 71 | +Jeśli uruchomimy powyższy kod, zobaczymy tylko pierwszy `alert`, a następnie otrzymamy komunikat błędu w konsoli! |
73 | 72 |
|
74 |
| -But everything is fine again if we add a semicolon after `alert`: |
| 73 | +Wszystko jednak zacznie działać, gdy tylko umieścimy średnik po pierwszej instrukcji: |
75 | 74 | ```js run
|
76 |
| -alert("All fine now"); |
| 75 | +alert("Wszystko jest teraz ok"); |
77 | 76 |
|
78 |
| -[1, 2].forEach(alert) |
| 77 | +[1, 2].forEach(alert) |
79 | 78 | ```
|
80 | 79 |
|
81 |
| -Now we have the "All fine now" message followed by `1` and `2`. |
| 80 | +Dostaniemy teraz najpierw komunikat z treścią "Wszystko jest teraz ok", a następnie dwa kolejne, o treści `1` i `2`. |
82 | 81 |
|
83 | 82 |
|
84 |
| -The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`. |
| 83 | +Problem z brakiem średnika w tym błędnym wariancie powstał, ponieważ JavaScript nie zakłada średnika przed nawiasem kwadratowym `[...]`. |
85 | 84 |
|
86 |
| -So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it: |
| 85 | +A skoro średnik nie jest automatycznie wstawiany, interpreter traktuje kod z pierwszego przykładu jako jedną instrukcję. Dla silnika JavaScript wygląda ona następująco: |
87 | 86 |
|
88 | 87 | ```js run no-beautify
|
89 |
| -alert("There will be an error")[1, 2].forEach(alert) |
| 88 | +alert("Tutaj będzie błąd")[1, 2].forEach(alert) |
90 | 89 | ```
|
91 | 90 |
|
92 |
| -But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations. |
| 91 | +To powinny być dwie oddzielne instrukcje, ale nie są. Takie łączenie jest po prostu błędne i może pojawić się również w wielu innych sytuacjach. |
93 | 92 | ````
|
94 | 93 |
|
95 |
| -We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them. |
| 94 | +Zalecamy używanie średników nawet wtedy, gdy instrukcje są oddzielone nową linią. Społeczność programistów przyjęła taką właśnie zasadę. Zanotujmy to jeszcze raz -- _istnieje możliwość_ nie wpisywania średników w większości przypadków. Ale bezpieczniej jest -- szczególnie dla początkujących -- używać ich zawsze. |
| 95 | + |
| 96 | +## Komentarze |
96 | 97 |
|
97 |
| -## Comments |
| 98 | +Z czasem programy stają się coraz bardziej złożone. Przychodzi wtedy konieczność dodania _komentarzy_, które opisują, co robi kod i dlaczego. |
98 | 99 |
|
99 |
| -As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why. |
| 100 | +Komentarze mogą być umieszczane w dowolnym miejscu skryptu. Nie wpływają na wykonanie kodu, ponieważ silnik JavaScript je ignoruje. |
100 | 101 |
|
101 |
| -Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them. |
| 102 | +**Komentarze jednoliniowe zaczynają się od podwójnego ukośnika (slasha) `//`.** |
102 | 103 |
|
103 |
| -**One-line comments start with two forward slash characters `//`.** |
| 104 | +Reszta linii to komentarz. Może zajmować całą linię od początku lub zostać umieszczony za instrukcją. |
104 | 105 |
|
105 |
| -The rest of the line is a comment. It may occupy a full line of its own or follow a statement. |
| 106 | +Tak jak tutaj: |
106 | 107 |
|
107 |
| -Like here: |
108 | 108 | ```js run
|
109 |
| -// This comment occupies a line of its own |
110 |
| -alert('Hello'); |
| 109 | +// Ten komentarz zaczyna się od początku |
| 110 | +alert("Witaj"); |
111 | 111 |
|
112 |
| -alert('World'); // This comment follows the statement |
| 112 | +alert("świecie"); // Ten komentarz umieszczony jest za instrukcją |
113 | 113 | ```
|
114 | 114 |
|
115 |
| -**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.** |
| 115 | +**Komentarze w kilku liniach zaczynamy od ukośnika i gwiazdki <code>/\*</code>, a kończymy gwiazdką i ukośnikiem <code>\*/</code>.** |
116 | 116 |
|
117 |
| -Like this: |
| 117 | +Tak jak tutaj: |
118 | 118 |
|
119 | 119 | ```js run
|
120 |
| -/* An example with two messages. |
121 |
| -This is a multiline comment. |
| 120 | +/* To jest przykładowy komentarz. |
| 121 | +Komentarz zajmuje kilka linii. |
122 | 122 | */
|
123 |
| -alert('Hello'); |
124 |
| -alert('World'); |
| 123 | +alert("Witaj"); |
| 124 | +alert("świecie"); |
125 | 125 | ```
|
126 | 126 |
|
127 |
| -The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute. |
| 127 | +Treść komentarza jest ignorowana, więc możemy w nim umieścić kod <code>/\* ... \*/</code>, który się nie wykona. |
128 | 128 |
|
129 |
| -Sometimes it can be handy to temporarily disable a part of code: |
| 129 | +Czasami przydaje się to, gdy chcemy tymczasowo wyłączyć pewną część kodu: |
130 | 130 |
|
131 | 131 | ```js run
|
132 |
| -/* Commenting out the code |
133 |
| -alert('Hello'); |
| 132 | +/* Zakomentowana część kodu |
| 133 | +alert('Witaj'); |
134 | 134 | */
|
135 |
| -alert('World'); |
| 135 | +alert("świecie"); |
136 | 136 | ```
|
137 | 137 |
|
138 |
| -```smart header="Use hotkeys!" |
139 |
| -In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl`. |
| 138 | +```smart header="Używaj skrótów klawiaturowych!" |
| 139 | +W większości edytorów można zamienić jedną linię w komentarz za pomocą klawiszy `key:Ctrl+/` lub kilka linii za pomocą `key:Ctrl+Shift+/` (zaznacz fragment kodu i po prostu wciśnij te klawisze). Jeśli korzystasz z Maca, zamiast `key:Ctrl` użyj `key:Cmd`. |
140 | 140 | ```
|
141 | 141 |
|
142 |
| -````warn header="Nested comments are not supported!" |
143 |
| -There may not be `/*...*/` inside another `/*...*/`. |
| 142 | +````warn header="Nie można zagnieżdżać komentarzy!" |
| 143 | +Nie można umieszczać jednego komentarza blokowego `/*...*/` wewnątrz innego `/*...*/`. |
144 | 144 |
|
145 |
| -Such code will die with an error: |
| 145 | +Taki kod rzuci błędem: |
146 | 146 |
|
147 | 147 | ```js run no-beautify
|
148 | 148 | /*
|
149 |
| - /* nested comment ?!? */ |
| 149 | + /* zagnieżdżony komentarz ?!? */ |
150 | 150 | */
|
151 |
| -alert( 'World' ); |
| 151 | +alert( 'świecie' ); |
152 | 152 | ```
|
153 | 153 | ````
|
154 | 154 |
|
155 |
| -Please, don't hesitate to comment your code. |
| 155 | +Nie bój się komentować swojego kodu. |
156 | 156 |
|
157 |
| -Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify code before publishing to a production server. They remove comments, so they don't appear in the working scripts. Therefore, comments do not have negative effects on production at all. |
| 157 | +Komentarze może i zwiększają objętość kodu, ale tym się nie martw. Istnieje wiele narzędzi, które minifikują kod przed opublikowaniem go w środowisku produkcyjnym poprzez m.in. usunięcie komentarzy. Korzystanie z takich narzędzi nie ma żadnego negatywnego wpływu na działanie skryptów w środowisku produkcyjnym. |
158 | 158 |
|
159 |
| -Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments. |
| 159 | +Później, w jednym z rozdziałów pt. "<info:code-quality>" poznasz, w jaki sposób pisać wartościowe komentarze. |
0 commit comments