Skip to content

Commit ce7708f

Browse files
authored
Translate 'Code structure' page (#21)
Original author: @bartek7
1 parent b1c1ce1 commit ce7708f

File tree

1 file changed

+70
-70
lines changed

1 file changed

+70
-70
lines changed

Diff for: 1-js/02-first-steps/02-structure/article.md

+70-70
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,159 @@
1-
# Code structure
1+
# Struktura kodu
22

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.
44

5-
## Statements
5+
## Instrukcje
66

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.
88

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!".
1010

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.
1212

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:
1414

1515
```js run no-beautify
16-
alert('Hello'); alert('World');
16+
alert("Witaj");
17+
alert("świecie");
1718
```
1819

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:
2021

2122
```js run no-beautify
22-
alert('Hello');
23-
alert('World');
23+
alert("Witaj");
24+
alert("świecie");
2425
```
2526

26-
## Semicolons [#semicolon]
27+
## Średniki [#semicolon]
2728

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.
2930

30-
This would also work:
31+
To także zadziała:
3132

3233
```js run no-beautify
33-
alert('Hello')
34-
alert('World')
34+
alert("Hello");
35+
alert("World");
3536
```
3637

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).
3839

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!**
4041

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:
4243

4344
```js run no-beautify
44-
alert(3 +
45-
1
46-
+ 2);
45+
alert(3 + 1 + 2);
4746
```
4847

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.
5049

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.**
5251

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.
5453

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:
5756
5857
```js run
5958
[1, 2].forEach(alert)
6059
```
6160
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`.
6362
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:
6564
6665
```js run no-beautify
67-
alert("There will be an error")
66+
alert("Tutaj będzie błąd")
6867
6968
[1, 2].forEach(alert)
7069
```
7170
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!
7372
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:
7574
```js run
76-
alert("All fine now");
75+
alert("Wszystko jest teraz ok");
7776
78-
[1, 2].forEach(alert)
77+
[1, 2].forEach(alert)
7978
```
8079
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`.
8281
8382
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 `[...]`.
8584
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:
8786
8887
```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)
9089
```
9190
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.
9392
````
9493

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
9697

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.
9899

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.
100101

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) `//`.**
102103

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ą.
104105

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:
106107

107-
Like here:
108108
```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");
111111

112-
alert('World'); // This comment follows the statement
112+
alert("świecie"); // Ten komentarz umieszczony jest za instrukcją
113113
```
114114

115-
**Multiline comments start with a forward slash and an asterisk <code>/&#42;</code> and end with an asterisk and a forward slash <code>&#42;/</code>.**
115+
**Komentarze w kilku liniach zaczynamy od ukośnika i gwiazdki <code>/\*</code>, a kończymy gwiazdką i ukośnikiem <code>\*/</code>.**
116116

117-
Like this:
117+
Tak jak tutaj:
118118

119119
```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.
122122
*/
123-
alert('Hello');
124-
alert('World');
123+
alert("Witaj");
124+
alert("świecie");
125125
```
126126

127-
The content of comments is ignored, so if we put code inside <code>/&#42; ... &#42;/</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.
128128

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:
130130

131131
```js run
132-
/* Commenting out the code
133-
alert('Hello');
132+
/* Zakomentowana część kodu
133+
alert('Witaj');
134134
*/
135-
alert('World');
135+
alert("świecie");
136136
```
137137

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`.
140140
```
141141

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 `/*...*/`.
144144
145-
Such code will die with an error:
145+
Taki kod rzuci błędem:
146146
147147
```js run no-beautify
148148
/*
149-
/* nested comment ?!? */
149+
/* zagnieżdżony komentarz ?!? */
150150
*/
151-
alert( 'World' );
151+
alert( 'świecie' );
152152
```
153153
````
154154

155-
Please, don't hesitate to comment your code.
155+
Nie bój się komentować swojego kodu.
156156

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.
158158

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

Comments
 (0)