|
1 | | -# Code structure |
| 1 | +# Struktura koda |
2 | 2 |
|
3 | | -The first thing we'll study is the building blocks of code. |
| 3 | +Prvo što ćemo proučiti pisanje blokova koda. |
4 | 4 |
|
5 | | -## Statements |
| 5 | +## Iskazi |
6 | 6 |
|
7 | | -Statements are syntax constructs and commands that perform actions. |
| 7 | +Iskazi su sintaksne konstrukcije i naredbe koje izvode akcije. |
8 | 8 |
|
9 | | -We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!". |
| 9 | +Već smo viđeli iskaz, `alert('Zdravo svijete!')`, koja prikazuje poruku "Zdravo svijete!". |
10 | 10 |
|
11 | | -We can have as many statements in our code as we want. Statements can be separated with a semicolon. |
| 11 | +U kodu možemo imati onoliko iskaza koliko želimo. Iskazi se mogu odvojiti tačkom sa zarezom. |
12 | 12 |
|
13 | | -For example, here we split "Hello World" into two alerts: |
| 13 | +Na primer, ovde smo podelili "Zdravo svijete!" na dva upozorenja: |
14 | 14 |
|
15 | 15 | ```js run no-beautify |
16 | | -alert('Hello'); alert('World'); |
| 16 | +alert('Zdravo'); alert('svijete!'); |
17 | 17 | ``` |
18 | 18 |
|
19 | | -Usually, statements are written on separate lines to make the code more readable: |
| 19 | +Iskazi se obično pišu u novim linijama kako bi se kod učinio čitljivijim: |
20 | 20 |
|
21 | 21 | ```js run no-beautify |
22 | | -alert('Hello'); |
23 | | -alert('World'); |
| 22 | +alert('Zdravo'); |
| 23 | +alert('svijete!'); |
24 | 24 | ``` |
25 | 25 |
|
26 | | -## Semicolons [#semicolon] |
| 26 | +## Tačka-zarez [#tackazarez] |
27 | 27 |
|
28 | | -A semicolon may be omitted in most cases when a line break exists. |
| 28 | +Tačka-zarez može biti izostavljena u većini slučajeva kada postoji prekid linije. |
29 | 29 |
|
30 | | -This would also work: |
| 30 | +Ovo bi takođe funkcionisalo: |
31 | 31 |
|
32 | 32 | ```js run no-beautify |
33 | | -alert('Hello') |
34 | | -alert('World') |
| 33 | +alert('Zdravo') |
| 34 | +alert('svijete') |
35 | 35 | ``` |
36 | 36 |
|
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). |
| 37 | +Ovde JavaScript interpretira proboj prelom linija kao "implicitni" tačka-zarez. To se naziva [automatsko umetanje tačake-zareza] |
| 38 | +(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 | +**U većini slučajeva novi red podrazumeva tačku sa zarezom. Ali "u većini slučajeva" ne znači "uvjek"!** |
40 | 41 |
|
41 | | -There are cases when a newline does not mean a semicolon. For example: |
| 42 | +Postoje slučajevi kada nova linija ne znači tačka-zarez. Na primjer: |
42 | 43 |
|
43 | 44 | ```js run no-beautify |
44 | 45 | alert(3 + |
45 | 46 | 1 |
46 | 47 | + 2); |
47 | 48 | ``` |
48 | 49 |
|
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. |
| 50 | +Kod ispisuje `6`, jer JavaScript ovde ne ubacuje tačku-zarez. Intuitivno je očigledno da ako se linija završava sa plus "+" `, onda je to" nepotpun izraz ", pa tačka-zarez nije obavezna. I u ovom slučaju to radi onako kako je planirano. |
50 | 51 |
|
51 | | -**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** |
52 | 52 |
|
53 | | -Errors which occur in such cases are quite hard to find and fix. |
| 53 | +**Ali postoje situacije u kojima JavaScript „ne uspijeva“ da pretpostavi tačku-zarez tamo gde je stvarno potrebno. |
| 54 | +** |
54 | 55 |
|
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: |
| 56 | +Greške koje se dešavaju u takvim slučajevima je stvarno teško otkriti i ispraviti. |
| 57 | + |
| 58 | +````smart header="Primjer greške" |
| 59 | +Ako ste radoznali da vidite konkretan primer takve greške, pogledajte ovaj kod: |
57 | 60 |
|
58 | 61 | ```js run |
59 | 62 | [1, 2].forEach(alert) |
60 | 63 | ``` |
61 | 64 |
|
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`. |
| 65 | +Još nije potrebno razmišljati o značenju zagrada `[]` i `forEach`. Kasnije ćemo ih proučiti. Za sada se samo sjetite rezultata koda: prikazuje "1", a zatim "2". |
63 | 66 |
|
64 | | -Now, let's add an `alert` before the code and *not* finish it with a semicolon: |
| 67 | +Sada, dodajmo upozorenje prije koda i *ne* završimo tačkom-zarezom: |
65 | 68 |
|
66 | 69 | ```js run no-beautify |
67 | | -alert("There will be an error") |
| 70 | +alert("Ovđe će doći do greške") |
68 | 71 |
|
69 | 72 | [1, 2].forEach(alert) |
70 | 73 | ``` |
71 | 74 |
|
72 | | -Now if we run the code, only the first `alert` is shown and then we have an error! |
| 75 | +Sada ako pokrenemo kod, prikazuje se samo prvo `upozorenje` i tada imamo grešku! |
73 | 76 |
|
74 | | -But everything is fine again if we add a semicolon after `alert`: |
| 77 | +Ali opet je sve u redu ako nakon `upozorenja` dodamo tačku-zarez: |
75 | 78 | ```js run |
76 | | -alert("All fine now"); |
| 79 | +alert("Sad je sve u redu!"); |
77 | 80 |
|
78 | 81 | [1, 2].forEach(alert) |
79 | 82 | ``` |
80 | 83 |
|
81 | | -Now we have the "All fine now" message followed by `1` and `2`. |
| 84 | +Sada imamo poruku "Sve u redu", a zatim slijede "1" i "2". |
| 85 | +
|
82 | 86 |
|
| 87 | +Do greške u varijanti koja nije pod tačkom-zarezom događa se jer JavaScript ne podrazumeva tačku sa zarezom ispred uglastih zagrada `[...]`. |
83 | 88 |
|
84 | | -The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`. |
| 89 | +Dakle, jer tačka-zarez nije automatski umetnuto, kod u prvom primeru se tretira kao jedna stavka. Evo kako motor to vidi: |
85 | 90 |
|
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: |
87 | 91 |
|
88 | 92 | ```js run no-beautify |
89 | | -alert("There will be an error")[1, 2].forEach(alert) |
| 93 | +alert("Ovđe će doći do greške!")[1, 2].forEach(alert) |
90 | 94 | ``` |
91 | 95 |
|
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. |
| 96 | +Ali to bi trebalo biti dva odvojena iskaza, ne jedan. Takvo spajanje u ovom slučaju je upravo pogrešno, otuda i greška. To se može dogoditi i u drugim situacijama. |
93 | 97 | ```` |
94 | 98 |
|
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. |
| 99 | +Preporučujemo stavljanje tačke-zareza između izjava, čak i ako su razdvojene novim linijama. Ovo pravilo široko prihvata zajednica. Napomenimo još jednom - *moguće je* izostavljati tačku-zarez većinu vremena. Ali sigurnije je - posebno za početnike - koristiti ih. |
96 | 100 |
|
97 | | -## Comments |
| 101 | +## Komentari |
98 | 102 |
|
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. |
| 103 | +Kako vrijeme prolazi, programi postaju sve složeniji. Potrebno je dodati *komentare* koji opisuju šta kod radi i zašto. |
100 | 104 |
|
101 | | -Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them. |
| 105 | +Komentari se mogu staviti na bilo koje mesto skripte. Oni ne utiču na njegovo izvršenje jer ih motor jednostavno ignoriše. |
102 | 106 |
|
103 | | -**One-line comments start with two forward slash characters `//`.** |
| 107 | +**Jednolinijski komentari počinju sa dvije prednje kose crte `//`.** |
104 | 108 |
|
105 | | -The rest of the line is a comment. It may occupy a full line of its own or follow a statement. |
| 109 | +Ostatak linije je komentar. Može zauzeti punu liniju ili slediti iskaz. |
106 | 110 |
|
107 | | -Like here: |
| 111 | +Kao što je ovđe: |
108 | 112 | ```js run |
109 | | -// This comment occupies a line of its own |
110 | | -alert('Hello'); |
| 113 | +// Ovaj komentar zauzima sopstvenu liniju |
| 114 | +alert('Zdravo'); |
111 | 115 |
|
112 | | -alert('World'); // This comment follows the statement |
| 116 | +alert('Svijete'); // Ovaj komentar prati iskaz |
113 | 117 | ``` |
114 | 118 |
|
115 | | -**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.** |
| 119 | +**Višelinijski komentari počinju sa jednom prednjom kosom crtom i zvjezdicom <code>/*</code> i završavaju se sa zvjezdicom i prednjom kosom crtom <code>*/</code>.** |
116 | 120 |
|
117 | | -Like this: |
| 121 | +Kao što je ovo: |
118 | 122 |
|
119 | 123 | ```js run |
120 | | -/* An example with two messages. |
121 | | -This is a multiline comment. |
| 124 | +/* Primjer sa dvije poruke |
| 125 | +Ovo je višelinijski komentar |
122 | 126 | */ |
123 | | -alert('Hello'); |
124 | | -alert('World'); |
| 127 | +alert('Zdravo'); |
| 128 | +alert('Svijete'); |
125 | 129 | ``` |
126 | 130 |
|
127 | | -The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute. |
128 | | - |
129 | | -Sometimes it can be handy to temporarily disable a part of code: |
| 131 | +Sadržaj komentara se ignoriše, pa ako stavimo kod unutra <code>/* ... */</code>, neće se izvršavati. |
130 | 132 |
|
| 133 | +Ponekad je korisno privremeno deaktivirati deo koda: |
131 | 134 | ```js run |
132 | | -/* Commenting out the code |
133 | | -alert('Hello'); |
| 135 | +/* Komentarisanje koda |
| 136 | +alert('Zdravo'); |
134 | 137 | */ |
135 | | -alert('World'); |
| 138 | +alert('Svijete'); |
136 | 139 | ``` |
137 | 140 |
|
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`. |
| 141 | +```smart header="Korišćenje tastera za prečice!" |
| 142 | +U većini urednika koda se može komentarisati pritiskom na `taster:Ctrl+/` za jednolinijski komentar i nešto slično `taster:Ctrl+Shift+/` -- za komentare sa više linija (izaberite deo koda i pritisnite taster). Probajte za Mac računare `taster:Cmd` umjesto `taster:Ctrl`. |
140 | 143 | ``` |
141 | 144 |
|
142 | | -````warn header="Nested comments are not supported!" |
143 | | -There may not be `/*...*/` inside another `/*...*/`. |
| 145 | +````warn header="Uneseni komentari nisu podržani!" |
| 146 | +Ne može biti `/*...*/` u `/*...*/`. |
144 | 147 |
|
145 | | -Such code will die with an error: |
| 148 | +Takav kod bit će uništen greškom |
146 | 149 |
|
147 | 150 | ```js run no-beautify |
148 | 151 | /* |
149 | | - /* nested comment ?!? */ |
| 152 | + /* komentar u komentar ?!? */ |
150 | 153 | */ |
151 | | -alert( 'World' ); |
| 154 | +alert( 'Svijet' ); |
152 | 155 | ``` |
153 | 156 | ```` |
154 | 157 |
|
155 | | -Please, don't hesitate to comment your code. |
| 158 | +Ne ustručavajte se da komentarišete svoj kod. |
156 | 159 |
|
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. |
| 160 | +Komentari povećavaju ukupni trag koda, ali to uopšte nije problem. Postoji mnogo alata koji minimiziraju kod prije objavljivanja na proizvodnom serveru. Oni uklanjaju komentare, tako da se ne pojavljuju u radnim skriptama. Stoga komentari uopšte nemaju negativne efekte na proizvodnju. |
158 | 161 |
|
159 | | -Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments. |
| 162 | +Kasnije će u udžbeniku biti poglavlja <info:code-quality> to takođe objašnjava kako napisati bolje komentare. |
0 commit comments