|
1 | | -# Code structure |
| 1 | +# Structura codului |
2 | 2 |
|
3 | | -The first thing we'll study is the building blocks of code. |
| 3 | +Primele lucruri pe care le vom studia sunt blocurile de bază ale codului. |
4 | 4 |
|
5 | | -## Statements |
| 5 | +## Declarații |
6 | 6 |
|
7 | | -Statements are syntax constructs and commands that perform actions. |
| 7 | +Declarațiile sunt construcții de sintaxă și comenzi care efectuează acțiuni. |
8 | 8 |
|
9 | | -We've already seen a statement, `alert('Hello, world!')`, which shows the message "Hello, world!". |
| 9 | +Deja am întâlnit o declarație, `alert('Hello, world!')`, care afișează mesajul "Hello, world!". |
10 | 10 |
|
11 | | -We can have as many statements in our code as we want. Statements can be separated with a semicolon. |
| 11 | +Putem avea cât de multe declarații vrem în codul nostru. Declarațiile pot fi separate prin punct și virgulă. |
12 | 12 |
|
13 | | -For example, here we split "Hello World" into two alerts: |
| 13 | +De exemplu, aici vom despărți "Hello World" în două alerte diferite: |
14 | 14 |
|
15 | 15 | ```js run no-beautify |
16 | 16 | alert('Hello'); alert('World'); |
17 | 17 | ``` |
18 | 18 |
|
19 | | -Usually, statements are written on separate lines to make the code more readable: |
| 19 | +De obicei, declarațiile sunt scrise pe linii separate pentru a face codul mai lizibil: |
20 | 20 |
|
21 | 21 | ```js run no-beautify |
22 | 22 | alert('Hello'); |
23 | 23 | alert('World'); |
24 | 24 | ``` |
25 | 25 |
|
26 | | -## Semicolons [#semicolon] |
| 26 | +## Punctul și virgula [#semicolon] |
27 | 27 |
|
28 | | -A semicolon may be omitted in most cases when a line break exists. |
| 28 | +Punctul și virgula pot fi omise în majoritatea cazurilor când există o pauză de linie. |
29 | 29 |
|
30 | | -This would also work: |
| 30 | +Acest lucru ar funcționa, de asemenea: |
31 | 31 |
|
32 | 32 | ```js run no-beautify |
33 | 33 | alert('Hello') |
34 | 34 | alert('World') |
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 | +Aici, JavaScript interpretează întreruperea liniei ca punct și virgulă „implicit”. Aceasta se numește [introducerea automata a punctului și virgulei](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion). |
38 | 38 |
|
39 | | -**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!** |
| 39 | +**În cele mai multe cazuri, o linie nouă implică punct și virgulă. Dar „în majoritatea cazurilor” nu înseamnă „întotdeauna!** |
40 | 40 |
|
41 | | -There are cases when a newline does not mean a semicolon. For example: |
| 41 | +Sunt cazuri când o linie nouă nu înseamna un punct și virgulă. De exemplu: |
42 | 42 |
|
43 | 43 | ```js run no-beautify |
44 | 44 | alert(3 + |
45 | 45 | 1 |
46 | 46 | + 2); |
47 | 47 | ``` |
48 | 48 |
|
| 49 | + |
49 | 50 | 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 a semicolon there would be incorrect. And in this case, that works as intended. |
50 | 51 |
|
51 | | -**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.** |
| 52 | +**Există situații în care JavaScript „eșuează” să-și asume un punct și virgulă unde este cu adevărat necesar.** |
52 | 53 |
|
53 | | -Errors which occur in such cases are quite hard to find and fix. |
| 54 | +Erorile care apar în astfel de cazuri sunt destul de greu de găsit și de remediat. |
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 | +````smart header="Exemplu de eroare" |
| 57 | +Dacă sunteți curios să vedeți un exemplu concret de astfel de eroare, verificați acest cod: |
57 | 58 |
|
58 | 59 | ```js run |
59 | 60 | alert("Hello"); |
@@ -86,70 +87,74 @@ alert("Hello")[1, 2].forEach(alert); |
86 | 87 | Looks weird, right? Such merging in this case is just wrong. We need to put a semicolon after `alert` for the code to work correctly. |
87 | 88 |
|
88 | 89 | This can happen in other situations also. |
| 90 | +
|
89 | 91 | ```` |
90 | 92 |
|
91 | | -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. |
| 93 | +Vă recomandăm să puneți punct și virgulă între instrucțiuni, chiar dacă sunt separate prin linii noi. Această regulă este adoptată pe scară largă de comunitate. Să notăm încă o dată -- *este posibil* să omitem punctul și virgula de cele mai multe ori. Dar este mai sigur -- mai ales pentru un începător -- să o folosească. |
| 94 | + |
92 | 95 |
|
93 | 96 | ## Comments [#code-comments] |
94 | 97 |
|
95 | | -As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why. |
96 | 98 |
|
97 | | -Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them. |
| 99 | +Pe măsură ce timpul trece, programele devin din ce în ce mai complexe. Devine necesar să adăugați *comentarii* care descriu ce face codul și de ce. |
98 | 100 |
|
99 | | -**One-line comments start with two forward slash characters `//`.** |
| 101 | +Comentariile pot fi puse în orice loc al unui script. Acestea nu afectează execuția acestuia, deoarece motorul pur și simplu le ignoră. |
100 | 102 |
|
101 | | -The rest of the line is a comment. It may occupy a full line of its own or follow a statement. |
| 103 | +**Comentariile pe o singură linie încep cu două caractere de slash înainte `//`.** |
102 | 104 |
|
103 | | -Like here: |
| 105 | +Restul liniei este un comentariu. Poate ocupa o linie completă proprie sau poate urma o declarație. |
| 106 | + |
| 107 | +Ca aici: |
104 | 108 | ```js run |
105 | | -// This comment occupies a line of its own |
| 109 | +// Acest comentariu ocupă o linie proprie |
106 | 110 | alert('Hello'); |
107 | 111 |
|
108 | | -alert('World'); // This comment follows the statement |
| 112 | +alert('World'); // Acest comentariu urmează afirmația |
109 | 113 | ``` |
110 | 114 |
|
111 | | -**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.** |
| 115 | +**Comentariile pe mai multe linii încep cu un slash înainte și un asterisc <code>/*</code> și se încheie cu un asterisc și cu un slash înainte <code>*/</code>.** |
112 | 116 |
|
113 | | -Like this: |
| 117 | +În felul următor: |
114 | 118 |
|
115 | 119 | ```js run |
116 | | -/* An example with two messages. |
117 | | -This is a multiline comment. |
| 120 | +/* Un exemplu cu două mesaje. |
| 121 | +Acesta este un comentariu pe mai multe linii. |
118 | 122 | */ |
119 | 123 | alert('Hello'); |
120 | 124 | alert('World'); |
121 | 125 | ``` |
122 | 126 |
|
123 | | -The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute. |
| 127 | +Conținutul comentariilor este ignorat, deci dacă punem cod în interior <code>/* ... */</code>, acesta nu o să se execute. |
124 | 128 |
|
125 | | -Sometimes it can be handy to temporarily disable a part of code: |
| 129 | +Uneori, poate fi util să dezactivați temporar o parte a codului: |
126 | 130 |
|
127 | 131 | ```js run |
128 | | -/* Commenting out the code |
| 132 | +/* Comentând codul |
129 | 133 | alert('Hello'); |
130 | 134 | */ |
131 | 135 | alert('World'); |
132 | 136 | ``` |
133 | 137 |
|
| 138 | + |
134 | 139 | ```smart header="Use hotkeys!" |
135 | 140 | 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` and `key:Option` instead of `key:Shift`. |
136 | 141 | ``` |
137 | 142 |
|
138 | | -````warn header="Nested comments are not supported!" |
139 | | -There may not be `/*...*/` inside another `/*...*/`. |
| 143 | +````warn header="Comentariile imbricate nu sunt suportate!" |
| 144 | +Nu poate exista `/*...*/` în interiorul altui `/*...*/`. |
140 | 145 |
|
141 | | -Such code will die with an error: |
| 146 | +Acest cod va genera o eroare: |
142 | 147 |
|
143 | 148 | ```js run no-beautify |
144 | 149 | /* |
145 | | - /* nested comment ?!? */ |
| 150 | + /* comentariu imbricat ?!? */ |
146 | 151 | */ |
147 | 152 | alert( 'World' ); |
148 | 153 | ``` |
149 | 154 | ```` |
150 | 155 |
|
151 | | -Please, don't hesitate to comment your code. |
| 156 | +Vă rugăm, nu ezitați să vă comentați codul. |
152 | 157 |
|
153 | | -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. |
| 158 | +Comentariile cresc amprenta generală a codului, dar aceasta nu este deloc o problemă. Există multe instrumente care reduc codul înainte de publicarea pe un server de producție. Șterg comentariile, deci nu apar în scripturile de lucru. Prin urmare comentariile nu au deloc efecte negative asupra producției. |
154 | 159 |
|
155 | | -Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments. |
| 160 | +Mai târziu în tutorial va exista un capitol <info:code-quality> care explică, de asemenea, cum să scrieți comentarii mai bune. |
0 commit comments